From 69e3eda6e5aa4631a7791cd378ada29b8d23deb7 Mon Sep 17 00:00:00 2001 From: aixianling Date: Fri, 19 Nov 2021 10:33:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/apps/index.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/apps/index.js diff --git a/package.json b/package.json index 0c4646af..116cb3b8 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "lib": "npm unpublish --force&&npm publish", "pages": "node bin/serve.js" }, + "main": "src/apps/index.js", "files": [ "src/components", "src/apps" diff --git a/src/apps/index.js b/src/apps/index.js new file mode 100644 index 00000000..993150d7 --- /dev/null +++ b/src/apps/index.js @@ -0,0 +1,33 @@ +const install = function (Vue, params) { + if (install.installed) return + // 遍历注册全局组件 + let apps = [] + let contexts = require.context('.', true, /\.(\/.+)\/App[^\/]+\.vue$/) + if (contexts) { + contexts.keys().map(e => { + if (contexts(e).default) { + if (params?.apps) { + if (params?.apps.includes(contexts(e).default.name)) { + apps.push(contexts(e).default) + Vue.component(contexts(e).default.name, contexts(e).default) + } + } else { + apps.push(contexts(e).default) + Vue.component(contexts(e).default.name, contexts(e).default) + } + } + }) + !!params?.showList && console.log(apps.map(e => e.name)) + } + return Promise.resolve(apps) +} + +// 判断是否是直接引入文件 +if (typeof window !== 'undefined' && window.Vue) { + install(window.Vue) +} + +export default { + // 导出的对象必须具有 install,才能被 Vue.use() 方法安装 + install +}