定义插件,install必须的
MyPlugin.install = function (Vue, options) {
// 1. add global method or property
Vue.myGlobalMethod = function () {
// something logic ...
}
// 2. add a global asset
Vue.directive('my-directive', {
bind (el, binding, vnode, oldVnode) {
// something logic ...
}
...
})
// 3. inject some component options
Vue.mixin({
created: function () {
// something logic ...
}
...
})
// 4. add an instance method
Vue.prototype.$myMethod = function (options) {
// something logic ...
}
}
使用插件Vue.use()
// calls `MyPlugin.install(Vue)`
Vue.use(MyPlugin)
Vue.use(MyPlugin, { someOption: true })
插件只能安装一次,vue会自动判断
官方插件vue-router 会自动调用
有时候在commonJs中需要自己调用
// When using CommonJS via Browserify or Webpack
var Vue = require('vue')
var VueRouter = require('vue-router')
// Don't forget to call this
Vue.use(VueRouter)