Vue use的理解


常見的用法有Vue.use(VueRouter); Vue.use(ElementUI)

先看看 vue的源碼

function initUse (Vue) {
    Vue.use = function (plugin) {
      var installedPlugins = (this._installedPlugins || (this._installedPlugins = []));
    //插件只能注冊一次,注冊后不再注冊
if (installedPlugins.indexOf(plugin) > -1) { return this } // additional parameters var args = toArray(arguments, 1);
    // 參數添加vue示例,在install函數使用 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 }; }

可以寫個簡單的插件

Vue.use({
    install:function(vue){
        vue.component('comp',{
        name: 'comp', template: `
<div>插件demo</div>` }) } })

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM