vue自定义插件

官网教程

官网链接

开发插件

插件通常会为 Vue 添加全局功能。插件的范围没有限制——一般有下面几种:
添加全局方法或者属性,如: vue-custom-element
添加全局资源:指令/过滤器/过渡等,如 vue-touch
通过全局 mixin 方法添加一些组件选项,如: vue-router
添加 Vue 实例方法,通过把它们添加到 Vue.prototype 上实现。
一个库,提供自己的 API,同时提供上面提到的一个或多个功能,如 vue-router
Vue.js 的插件应当有一个公开方法 install 。这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象:

MyPlugin.install = function (Vue, options) {
  // 1. 添加全局方法或属性
  Vue.myGlobalMethod = function () {
    // 逻辑...
  }

  // 2. 添加全局资源
  Vue.directive('my-directive', {
    bind (el, binding, vnode, oldVnode) {
      // 逻辑...
    }
    ...
  })

  // 3. 注入组件
  Vue.mixin({
    created: function () {
      // 逻辑...
    }
    ...
  })

  // 4. 添加实例方法
  Vue.prototype.$myMethod = function (methodOptions) {
    // 逻辑...
  }
}

使用插件

通过全局方法 Vue.use() 使用插件:
// 调用 MyPlugin.install(Vue)
Vue.use(MyPlugin)
也可以传入一个选项对象:
Vue.use(MyPlugin, { someOption: true })
Vue.use 会自动阻止多次注册相同插件,届时只会注册一次该插件。
Vue.js 官方提供的一些插件 (例如 vue-router) 在检测到 Vue 是可访问的全局变量时会自动调用 Vue.use()。然而在例如 CommonJS 的模块环境中,你应该始终显式地调用 Vue.use():
// 用 Browserify 或 webpack 提供的 CommonJS 模块环境时
var Vue = require(‘vue’)
var VueRouter = require(‘vue-router’)

// 不要忘了调用此方法
Vue.use(VueRouter)
awesome-vue 集合了来自社区贡献的数以千计的插件和库。

根据官网教程自己写个例子

新建一个.vue 的页面aa.vue

<template>
    <div>
      <h1>loadding</h1>
    </div>
</template>

<script>
export default {
  name: 'aa'
}
</script>

<style scoped>

</style>

然后把这 aa.vue 做成插件,如何做呢?再建一个.js,下面是根据官网改写的,新加了一句Vue.component('Loading', loading)

import loading from './aa'
const MyPlugin = {
  install: function (Vue, options) {
    Vue.component('Loading', loading)
    // 1. 添加全局方法或属性
    Vue.myGlobalMethod = function () {
      console.log('ddddddd')
      // 逻辑...
    }

    // 2. 添加全局资源
    Vue.directive('my-directive', {
      bind (el, binding, vnode, oldVnode) {
       el.style.backgroundColor = binding.value
        // 逻辑...
      }
    })

    // 3. 注入组件
    Vue.mixin({
      created: function () {
        // 逻辑...
      }
    })

    // 4. 添加实例方法
    Vue.prototype.$myMethod = function (methodOptions) {
      console.log('ccccccccc')
      // 逻辑...
    }
  }
}

export default MyPlugin

插件做好了,就这么简单。下面说说上面的几个方法
Vue.component('Loading', loading)定义一个自定义组件,在 main.js应用后,可以在任何地方应用这个组件
Vue.myGlobalMethod定义 Vue 一个全局的方法,后面我会出代码说怎么用。

Vue.directive('my-directive',定义自定义的指令,后面有个简单例子,可以看官网
Vue.mixin({混入,这个不多说,应用中应用的不多。官网地址
Vue.prototype.$myMethod自定义的实例方法,可以在.vue 中利用 this.myMethod 调用。

应用组件

修改 main.js,首先先引入自定义的插件import my from '../test/vueinstall',然后利用 Vue.use 应用插件Vue.use(my)

修改后如下

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import my from '../test/vueinstall'

Vue.config.productionTip = false
Vue.use(my)
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

新建.vue文件,在 vue文件中应用插件。

<template>
  <div class="hello">
    <Loading v-my-directive="messages"></Loading>// 应用组件 Loading
    <div v-my-directive="messages">aaaaa</div>//应用自定义插件
  </div>
</template>

<script>
import Vue from 'vue'
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      messages: 'red'
    }
  },
  mounted () {
    Vue.myGlobalMethod()//vue 全局方法使用
    this.$myMethod()//在实例中使用
  },
  methods: {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

这就是自定义的插件使用,同样在使用其他的插件也是这样,比如 element-ui,

import Element from 'element-ui'
Vue.use(Element)

这样就可以全局使用 element 的组件了。

猜你喜欢

转载自blog.csdn.net/u014196765/article/details/82756033