vue.js plug-in is usually used to add global functions to Vue. The function scope of the plug-in is not strictly limited. To use the plug-in through the global method [Vue.use()], you need to call [new Vue( )】Complete before starting the application.
[Related article recommendations: vue.js]
Plug-ins are usually used to add globals to Vue Function. There are no strict restrictions on the functional scope of plug-ins - generally there are the following types:
Add global methods or properties. For example: vue-custom-element
Add global resources: directives/filters/transitions, etc. For example, vue-touch
adds some component options through global mixing. For example, vue-router
adds Vue instance methods by adding them to Vue.prototype.
A library that provides its own API while providing one or more of the functions mentioned above. For example, vue-router
Use plug-ins
Use plug-ins through the global method Vue.use(). 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 plugin. , then 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:
// 用 Browserify 或 webpack 提供的 CommonJS 模块环境时 var Vue = require('vue') var VueRouter = require('vue-router') // 不要忘了调用此方法 Vue.use(VueRouter)
awesome-vue has a large collection of community-contributed plugins and libraries.
Develop plug-ins
Vue.js plug-ins should expose an install
method. The first parameter of this method is the Vue constructor, and the second parameter is an optional options object:
MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或 property Vue.myGlobalMethod = function () { // 逻辑... } // 2. 添加全局资源 Vue.directive('my-directive', { bind (el, binding, vnode, oldVnode) { // 逻辑... } ... }) // 3. 注入组件选项 Vue.mixin({ created: function () { // 逻辑... } ... }) // 4. 添加实例方法 Vue.prototype.$myMethod = function (methodOptions) { // 逻辑... } }
Related free learning recommendations: JavaScript (video)
The above is the detailed content of What does vue.js plug-in mean?. For more information, please follow other related articles on the PHP Chinese website!