vue3+vue-router@4 Error: Invalid navigation guard 或者 The “next“ callback was never called inside of

snows_l's BLOGhttp://snows-l.site

一、报错样例

        如下:

二、问题出处

        原因是以为路由前置守卫使用了async await导致的,代码如下:

三、解决办法

         将async await 改成 .then的方式就不会报错咯

// 路由后置守卫
router.beforeEach((to, from, next) => {
  console.log('=========== to ===========', to.path);
  NProgress.start();
  if (getToken()) {
    const store = useAppStore();
    if (!store.isLoadingRouter) {
      store.getUserMenu().then(res => {
        const menus = generateRoutes(res);
        // 没有pc菜单权限
        if (menus[0].children.length) {
          menus.forEach((item, index) => {
            item.children &&
              item.children.forEach(subItem => {
                router.addRoute(index == 0 ? 'RouterView' : 'mRouterView', subItem);
              });
          });
          if (!router.hasRoute('NotFound')) {
            router.addRoute({
              name: 'NotFound',
              path: '/:pathMatch(.*)*',
              redirect: '/404'
            });
          }
          // console.log('=========== router.getRouters ===========', router.getRoutes());
          // 添加了路由
          routeNext(to, next, store, true);
        } else {
          router.push({ path: '/403' });
          store.isLoadingRouter = true;
        }
      });
    } else {
      routeNext(to, next, store);
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单,直接进入
      next();
    } else {
      if (isMobile()) {
        if (to.path === '/' || to.path === '/m') {
          next({ path: '/m/index' });
        } else {
          next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
        }
      } else {
        next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
      }
    }
  }
});

去掉之后刷新就不会报错咯

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__冬七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值