作用:它是Vue提供一个静态方法,用来向Vue注册插件(增强vue的功能)
使用:
- Vue.use 可以接收一个对象,Vue.use(obj)
- 对象obj中需要提供一个 install 函数
- 在 Vue.use(obj) 时,会自动调用该 install 函数,并传入 Vue构造器
const MyPlugin = {
install(Vue) {
console.log(Vue) //这行代码在打开页面就会显示
Vue.prototype.myFn=alert(这是我注册的插件) 这个方法需要手动调用
}
}
Vue.use(MyPlugin)
然后就可以在组件中调用这个方法
例如
Del(id) {
this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.myFn() // 调用Vue注册的方法弹出alert框
this.doDel(id)
}).catch(() => {
})
},
也可以用Vue.use 注册全局组件
import Lang from './Lang/index'
export default {
install: function(Vue) {
console.log('我们自定义的插件...')
Vue.component('Lang', Lang)
}
}
//在main.js导入并注册
import MyUI '@/components/index.js' // 导入
Vue.use(MyUI) //注册全局组件!