feat: 前端自动注册插件功能

This commit is contained in:
H0nGzA1
2023-03-31 00:25:23 +08:00
parent 96ad956efd
commit 255c405e59
3 changed files with 30 additions and 11 deletions

View File

@@ -0,0 +1,15 @@
import { defineAsyncComponent, AsyncComponentLoader } from 'vue';
// 扫描插件目录并注册插件
export const scanAndInstallPlugins = (app: any) => {
const components = import.meta.glob('./**/*.vue');
const pluginNames = new Set();
// 遍历对象并注册异步组件
for (const [key, value] of Object.entries(components)) {
const name = key.slice(key.lastIndexOf('/') + 1, key.lastIndexOf('.'));
app.component(name, defineAsyncComponent(value as AsyncComponentLoader));
const pluginsName = key.match(/\/([^\/]*)\//)?.[1];
pluginNames.add(pluginsName);
}
console.log('已发现插件:', Array.from(pluginNames));
};