1.定义
功能:用于增强Vue
本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据
定义插件的方法:
对象.insatll =function(Vue,options){
//1.添加全局过滤器
Vue.filter(...)
//2.添加全局指令
Vue.directive(...)
//3.配置全局混入
Vue.mixin(...)
//4.添加实例方法
Vue.prototype.demo =()=>{
alert('hello')
}
}
使用插件
Vue.use()
2.示例
创建一个plugins的js文件
import Vue from "vue"
export default {
install(){
Vue.prototype.demo =()=>{
alert('hello')
}
}
}
并在main.js中引入plugins.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import plugins from './plugins'
Vue.config.productionTip = false
Vue.use(plugins)
new Vue({
router,
render: h => h(App)
}).$mount('#app')
这样就可以全局使用插件了
在实际开发中我们会用到很多的插件,原理使用和引入的原理都是一样的