首页 > 解决方案 > “通过导航守卫从 {route} 重定向到 {another route}。” 在进行第二次重定向后

问题描述

我在使用我的代码进行重定向时遇到问题。

我有这样的事情:

store/auth.js:

async login(credentials) {
  const user = await api.post(credentials);
  if (user.role === 'someRole') {
     router.push({name: 'warehouses'});
  }
}

我将用户重定向到仓库,这很好,但是当我尝试再次将用户重定向到他们自己的仓库时:

warehouses.vue

  async beforeRouteEnter(to, from, next) {
    const user = store.getters.getUser;
    const warehouses = await api.get('warehouses');
    const userWarehouse = getWarehouseOwner(user, warehouses);
    
    if (userWarehouse) {
      next({name: 'warehouse', params: {id: userWareHouse }})';
    }
  }

然后我收到一个错误:

Uncaught (in promise) Error: Navigation cancelled from "/login" to "/warehouses" with a new navigation.

我阅读了有关此特定错误的信息,看起来不受支持。我怎样才能绕过这个错误?有没有办法可以执行第二次重定向?

标签: vue.jsvue-router

解决方案


推荐阅读