vue-router 重定向不适用于路由推送

vue-router redirect not working on route push

我配置了以下路由器:

const router = new Router({
  mode: 'history',

  routes: [
    // root
    {
      path: '/',
      component: ComponentPage,
      redirect: routePrefix.public,
      children: [

        // public
        {
          path: routePrefix.public,
          component: ComponentPage,
          redirect: `${routePrefix.public}/login`,
          children: [
            {
              path: `${routePrefix.public}/login`, component: LoginPage,
            }],
        },

        // private
        {
          path: routePrefix.private,
          component: ComponentPage,
          children: [
            {
              path: `${routePrefix.private}/u`, component: PrivatePage,
            }],
        }],
    }],
});

现在如您所见,主要有两个部分;一个 public 和一个私人的。 此外,我还为授权配置了以下导航守卫:

router.beforeEach((to, from, next) => {
  console.log(`registered request to redirect from 
    '${from.fullPath}' to '${to.fullPath}'`);

  if (to.fullPath.startsWith(routePrefix.private) &&
    !Store.getters['auth/isAuthenticated']) {
    console.log('-> reroute to public main');
    next(routePrefix.public);
  } else if (to.fullPath.startsWith(routePrefix.public) &&
    Store.getters['auth/isAuthenticated']) {
    console.log('-> reroute to private main');
    next(routePrefix.private);
  } else {
    next();
  }
});

如果您想知道路由前缀是什么样的,请看这里:

const routePrefix = {
  public: '/p', private: '/x',
};

没什么特别的。

问题

我打开页面 localhost:8080/,它按预期将 / 重定向到 /p/login。成功登录后,我执行 Router.push('/') 以进一步重新路由用户。

想法是 / 应该再次重定向到 /p/login,导航守卫会在此处启动并将其重定向到 /x/... 但事实并非如此。它停留在 /.

不是应该将其重定向到主页之外吗?我该如何解决?

我没有找到解决这个问题的方法。我已经通过直接推送到所需的目的地而不是让重新路由发挥其魔力来达到我的目标。这行得通。