扫描文件夹目录
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
const routers = [];
const contexts = require.context('@/application', true, /index.vue$/,'lazy');
contexts.keys().forEach(value => {
const path = value.substring(value.indexOf('/'), value.lastIndexOf('/'));
if (!path.split('/').includes('components')) {
const componentLocation = value.substring(value.indexOf('.') + 1);
const componentName = path.substring(1);
routers.push({
path: `/${componentName}`,
name: componentName,
component: () => import(`@/application${componentLocation}`)
});
}
});
const routes: Array<RouteRecordRaw> = [...routers];
const router = createRouter({
history: createWebHashHistory(),
routes
});
export default router;