浅谈Vue.use的使用(2)

export const Plugin = { install(Vue) { Vue.component... Vue.mixins... Vue... // 我们也可以在install里面执行其他函数,Vue会将this指向我们的插件 console.log(this) // {install: ...,utils: ...} this.utils(Vue) // 执行utils函数 console.log(this.COUNT) // 0 }, utils(Vue) { Vue... console.log(Vue) // Vue }, COUNT: 0 } // 我们可以在这个对象上添加参数,最终Vue只会执行install方法,而其他方法可以作为封装install方法的辅助函数 const test = 'test' export function Plugin2(Vue) { Vue... console.log(test) // 'test' // 注意如果插件编写成函数形式,那么Vue只会把this指向null,并不会指向这个函数 console.log(this) // null } // 这种方式我们只能在一个函数中编写插件逻辑,可封装性就不是那么强了

小弟不才,对vue源码的理解暂且到这。欢迎大佬们多指教~

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/16c5c222f413cd18ce575cb340af962b.html