Files
dvcp_v2_webapp/examples/router/autoRoutes.js

44 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-12-14 18:36:19 +08:00
import store from "../store";
2022-04-27 18:18:57 +08:00
import appEntry from "../views/appEntry";
import {waiting} from "../utils";
2022-04-27 19:36:32 +08:00
import router from "./router";
2021-12-14 18:36:19 +08:00
export default {
2022-04-27 18:18:57 +08:00
routes: [],
2021-12-14 18:36:19 +08:00
init() {
//约束正则式
store.commit("cleanApps")
2022-04-27 18:18:57 +08:00
this.routes = []
2021-12-14 18:36:19 +08:00
// 自动化本工程应用
2022-04-27 18:18:57 +08:00
return this.loadApps()
2021-12-14 18:36:19 +08:00
},
loadApps() {
2022-04-27 18:18:57 +08:00
//锁屏loading
waiting.init({innerHTML: '应用加载中..'})
2021-12-14 18:36:19 +08:00
//新App的自动化格式
2022-04-27 18:18:57 +08:00
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$/, '/$1'),
component: appEntry,
module: file.default
}
//命名规范入口文件必须以App开头
waiting.setContent(`加载${name}...`)
2022-04-27 19:36:32 +08:00
router.addRoute(addApp)
2022-04-27 18:18:57 +08:00
this.routes.push(addApp)
return store.commit("addApp", addApp)
})
} else {
return Promise.resolve()
2021-12-14 18:36:19 +08:00
}
2022-04-27 18:18:57 +08:00
})).then(() => waiting.close())
2021-12-14 18:36:19 +08:00
}
}