require.context

import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import Vue from "vue";

const routes = {};

const requireComponent = require.context("./src", true, /index\.js$/);

requireComponent.keys().forEach((key) => {
  const name = key.slice(2, -9);
  console.log(name);
  if (/^.\/[A-Za-z0-1]+\/index.js$/.test(key)) {
    routes[name] = requireComponent(key).default || requireComponent(key);

    Vue.component(name, routes[name]);
  }
});

// 定义 install 方法
const install = function (Vue, option = {}) {
  if (install.installed) return;
  install.installed = true;
  Vue.use(ElementUI, option);
};
if (typeof window !== "undefined" && window.Vue) {
  install(window.Vue);
}
// console.log(...Object.values(routes));
export default {
  // 导出的对象必须具备一个 install 方法
  install,
  ...Object.values(routes)
};

posted @ 2021-12-24 16:56  7c89  阅读(36)  评论(0编辑  收藏  举报