// 如果有动态路由可以参考
/* 首次或者刷新界面,next(...to, replace: true)会循环遍历路由
如果to找不到对应的路由会再执行一次beforeEach((to, from, next))直到找到对应路由 */

//路由守卫中添加
if (registerRouteFresh && to.path !== '/login' && to.path !== '/noRight') {
    await Http.get('/eoap/menu/list').then(async (res) => {
        if (res.data.code === 200) {
            // 获取用户路由
            userRoutes = res.data.data;
        }
        if (!userRoutes && userRoutes.length === 0) {
            next('/noRight');
        } else {
          	let newRoutes = transformRoute(); //如果需要,执行转换格式方式
            const newRoutes = routerInit(userRoutes);
            allRoutes.push({
                name: 'index',
                path: '/',
                component: () => import('@/views/index.vue'),
                children: newRoutes,
            });
            allRoutes.forEach((value, index) => {
                router.addRoute(value);
            });
            const notFound = {
                path: '/:catchAll(.*)*',
                redirect: { name: 'error' },
            };
            router.addRoute(notFound);
            next({ ...to, replace: true });
            registerRouteFresh = false;
        }
    });
} else {
    next();
}
Last Updated:
Contributors: shenxin