Files
dvcp_v2_webapp/examples/router/autoRoutes.js
2022-05-10 11:27:19 +08:00

44 lines
1.5 KiB
JavaScript

import store from "../store";
import appEntry from "../views/appEntry";
import {waiting} from "../utils";
import router from "./router";
export default {
routes: [],
init() {
//约束正则式
store.commit("cleanApps")
this.routes = []
// 自动化本工程应用
return this.loadApps()
},
loadApps() {
//锁屏loading
waiting.init({innerHTML: '应用加载中..'})
//新App的自动化格式
let files = import.meta.glob('../../packages/**/App*.vue')
let cores = import.meta.glob('../../core/**/App*.vue')
let projects = import.meta.glob('../../project/**/App*.vue')
files = {...files, ...cores, ...projects}
return Promise.all(Object.keys(files).map(path => {
if (/App[A-Z]\w+\.vue/.test(path)) {
return files?.[path]()?.then(file => {
let {name, label} = file.default,
addApp = {
name: [path.replace(/[.\/]+(project)?[\/]([a-z]+).+/, '$2'), name].join("_"), label: label || name,
path: path.replace(/[.\/]+([a-zA-Z].+\/App[A-Z]\w+)\.vue$/, '/v/$1'),
component: appEntry,
module: file.default
}
//命名规范入口文件必须以App开头
waiting.setContent(`加载${name}...`)
router.addRoute(addApp)
this.routes.push(addApp)
return store.commit("addApp", addApp)
})
} else {
return Promise.resolve()
}
})).then(() => waiting.close())
}
}