Skip to content

全局注册组件 vue use 干了什么

全局组件

js
const install = (Vue) => {
  Vue.components("base-select", ***);
};

export default install;

Vue.use(comp);

vue.use 干了什么

js
// Vue.use(module, options);

// 执行 module 中的 intall 方法,并将 Vue 传递进去, 并将 options 作为 module 参数传递进去

function initUse(Vue) {
  Vue.use = function (plugin) {
    var installedPlugins =
      this._installedPlugins || (this._installedPlugins = []);
    /* 判断过这个插件是否已经安装 */
    if (installedPlugins.indexOf(plugin) > -1) {
      return this;
    }
    var args = toArray(arguments, 1);
    args.unshift(this);
    /* 判断插件是否有 install 方法 */
    if (typeof plugin.install === "function") {
      plugin.install.apply(plugin, args);
    } else if (typeof plugin === "function") {
      plugin.apply(null, args);
    }
    installedPlugins.push(plugin);
    return this;
  };
}

在 MIT 许可下发布