plug-in


Directory


Plug-ins are usually used to add global functionality to Vue. There are no strict restrictions on the functional scope of plug-ins - generally there are the following types:

 1. Add global methods or attributes. Such as: vue-custom-element

2. Add global resources: instructions/filters/transitions, etc. Such as vue-touch

 3. Add some component options through global mixing. Such as vue-router

4. Add Vue instance methods by adding them to Vue.prototype.

 5. A library that provides its own API and provides one or more of the functions mentioned above. For example, vue-router


## uses the plug-in


through the global Method

Vue.use() Use plugin. It needs to be completed before you call new Vue() to start the application:

// 调用 `MyPlugin.install(Vue)`
Vue.use(MyPlugin)
new Vue({
  // ...组件选项
})

You can also pass in an optional options object:

Vue.use(MyPlugin, { someOption: true })

Vue. use will automatically prevent multiple registrations of the same plug-in, and the plug-in will only be registered once even if called multiple times.

Some plug-ins officially provided by Vue.js (such as

vue-router) will automatically call Vue.use()## when detecting that Vue is an accessible global variable. #. However in a module environment like CommonJS you should always call Vue.use() explicitly: Contributed plugins and libraries.

Develop plug-ins


Vue.js plug-ins should expose a install method. The first parameter of this method is the Vue

constructor, and the second parameter is an optional options object:

// 用 Browserify 或 webpack 提供的 CommonJS 模块环境时
var Vue = require('vue')
var VueRouter = require('vue-router')

// 不要忘了调用此方法
Vue.use(VueRouter)