使用浏览器导航栏的后退功能,会一直触发beforEach,陷入死循环,最后堆栈溢出
代码大致如下
router.beforeEach((to, from, next) => {
console.log('to----name---->',to.name)
if (_.find(loginRoute2, ['name', to.name])) {
next();
return;
}
const menuPurview = vueCookies.get(projectName + "MenuPurview");
const menuArray = menuPurview && JSON.parse(menuPurview) || []
const staticRouteData = getRoute(staticRoute, menuArray);
const routeDatas = _.concat(staticRouteData, loginRoute)
router.addRoutes(routeDatas); // 动态添加路由
router.options.routes = routeDatas;
if ((!menuArray.length && vueCookies.get(projectName + 'Token'))) {
next({
name: 'purview',
query: {
id: 1,
}
});
return;
} else if (!cookieRoutes) {
const staticRouteData = getRoute(staticRoute, menuArray);
const routeDatas = _.concat(staticRouteData, loginRoute)
router.addRoutes(routeDatas); // 动态添加路由
vueCookies.set(projectName + "RouterDatas", routeDatas);
router.options.routes = routeDatas;
let _to;
if (_.indexOf(menuPurview, to.name) == -1) {
_to = routeDatas[0] && routeDatas[0].children && routeDatas[0].children[0] || routeDatas[0] || null;
} else {
_to = to;
}
_to ? next({
..._to,
replace: true
}) : devToast({
message: '暂无权限,请联系管理员!'
})
} else {
next();
}
})
应该是这里的问题,_to值为假,也应该调用 next();