关于vuerouter的问题?

使用浏览器导航栏的后退功能,会一直触发beforEach,陷入死循环,最后堆栈溢出

image.png
代码大致如下

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();
     }
 })
阅读 1.7k
1 个回答
_to ? next({
     ..._to,
     replace: true
 }) : devToast({
     message: '暂无权限,请联系管理员!'
 })

应该是这里的问题,_to值为假,也应该调用 next();

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题