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}`); // 否则全部重定向到登录页
      }
    }
  }
});

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

a navigation guard. This error message is related to the use of navigation guards in the Vue.js router. Navigation guards are functions that are used to intercept and control navigation to different routes in your application. They can be used to prevent unauthorized access to certain pages, redirect users to specific pages, or perform other actions before the navigation is completed. The "next" callback is a function that must be called inside a navigation guard to allow the navigation to proceed. If the "next" callback is not called, the navigation will not be completed, and the user will be stuck on the current page. The error message "The 'next' callback was never called inside of a navigation guard" indicates that the "next" callback function was not called inside one of your navigation guards. To resolve this error, make sure that you call the "next" callback function inside each of your navigation guards. Here is an example of a navigation guard that uses the "next" callback: ``` router.beforeEach((to, from, next) => { // Check if the user is authenticated if (isAuthenticated()) { // Allow the navigation to proceed next() } else { // Redirect the user to the login page next('/login') } }) ``` In this example, the "next" callback function is called either to allow the navigation to proceed or to redirect the user to the login page if they are not authenticated. Make sure to include the "next" callback function in your navigation guards to avoid the "next" callback was never called error.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

冬零七

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

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

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

打赏作者

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

抵扣说明:

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

余额充值