Vue.use的时候做了什么?

1|0在vue的main中,我们经常用到插件,插件都是用Vue.use(plugin)进行注册的,那么在use的时候到底做了什么?

当我们调用use的时候,会调用插件中的install(设置)函数,并将vue实例传入install方法,

但是同一个插件只会在项目中安装一次,所以,其内部还有的作用就是防止同一插件被安装多次。

所以当Use插件名称时,会进行一个判断,判断插件数组中有没有这个值,没有就push进去,有就返回

Vue.use = function (plugin) { const installedPlugins = (this._installedPlugins || (this._installedPlugins = [])) if (installedPlugins.indexOf(plugin) > -1) { return this } // additional parameters const args = toArray(arguments, 1) args.unshift(this) if (typeof plugin.install === 'function') { plugin.install.apply(plugin, args) } else if (typeof plugin === 'function') { plugin.apply(null, args) } installedPlugins.push(plugin) return this }

1|1例如:自定义全局指令 用vue.use()注册 

定义一个对象,对象里面有install()方法,install方法里面用Vue的自定义指令

const directiveFocus = { install(Vue) { Vue.directive("fofo", { inserted(el) { el.focus(); }, }); }, }; Vue.use(directiveFocus)

 


__EOF__

本文作者长安
本文链接https://www.cnblogs.com/jingxin01/p/16616510.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   长安·念  阅读(69)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示