使用插件
- 通过全局方法
Vue.use()
使用插件。 - 它需要在你调用
new Vue()
启动应用之前完成 Vue.use
会自动阻止多次注册相同插件,届时即使多次调用也只会注册一次该插件。
import VueRouter from 'vue-router';
import Vue from 'vue';
import Antd from 'ant-design-vue';
import Vuex from 'vuex';
Vue.use(VueRouter)
Vue.use(Vuex )
Vue.use(Antd)
new Vue({
})
开发插件
- Vue.js 的插件暴露了一个 install 方法。
- 这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象。
MyPlugin.install = function (Vue, options) {
Vue.myGlobalMethod = function () {
}
Vue.directive('my-directive', {
bind (el, binding, vnode, oldVnode) {
}
...
})
Vue.mixin({
created: function () {
}
...
})
Vue.prototype.$myMethod = function (methodOptions) {
}
}