动态路由配置:
原动态路由:
let titleValue;
for (const x in rows) {
titleValue = rows[x].processName + '(' + rows[x].taskName + ')';
router.addRoute({
hidden: true,
component: Layout,
path: '/link/' + rows[x].formKey,
children: [{
path: 'index/:status_task',
name: 'link' + rows[x].formKey,
meta: {
title: titleValue,
activeMenu: getActiveUrl(rows[x].formUrl)
},
component: () => import('../views/' + rows[x].formUrl)
}]
});
}
打包部署Nginx环境后发现不能正常跳转,通过 import.meta.glob 语法改造。
现动态路由:
let titleValue;
const modules = import.meta.glob(`../views/**/*.vue`);
for (const x in rows) {
titleValue = rows[x].processName + '(' + rows[x].taskName + ')';
router.addRoute({
hidden: true,
component: Layout,
path: '/link/' + rows[x].formKey,
children: [{
path: 'index/:status_task',
name: 'link' + rows[x].formKey,
meta: {
title: titleValue,
activeMenu: getActiveUrl(rows[x].formUrl)
},
component: modules['../views/' + rows[x].formUrl + '.vue']
}]
});
}
index.js的router改造
原路由配置
const router = createRouter({
// base: '/production',
history: createWebHistory(),
routes: constantRoutes,
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { top: 0 }
}
},
});
改造后配置:
const router = createRouter({
// base: '/production',
history: createWebHashHistory(),
routes: constantRoutes,
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { top: 0 }
}
},
});