简体   繁体   English

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

[英]vue-router redirect not working on route push

I have the following router configured: 我配置了以下路由器:

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,
            }],
        }],
    }],
});

now as you can see, there are two main parts; 如您所见,现在有两个主要部分: a public, and a private one. 一个公共的和一个私人的。 Additionally I have the following navigation guard configured for authorization: 另外,我为授权配置了以下导航保护:

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();
  }
});

If you're wondering what the route prefix look like, here you go: 如果您想知道路由前缀是什么样的,请执行以下操作:

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

Nothing so special. 没什么特别的。

The problem 问题

I open the page localhost:8080/ which redirects / to /p/login as expected. 我打开页面localhost:8080/ ,按预期将/重定向到/p/login After a successful login, I perform a Router.push('/') with the intention to further re-route the user once again. 成功登录后,我执行Router.push('/')以便进一步重新路由用户。

The idea is that / should get redirect to /p/login again, where the navigation guard kicks in and redirects it to /x/ ... But it doesn't. 这个想法是/应该再次重定向到/p/login ,导航护卫会在其中启动并将其重定向到/x/ ...但事实并非如此。 It stays on / . 它停留在/

Isn't it supposed to redirect it away from the main page? 它不是应该将其重定向离开主页吗? How can I fix it? 我该如何解决?

I didn't find a solution to this problem. 我没有找到解决此问题的方法。 I've reached my goal by pushing to the desired destination directly instead of letting the rerouting do its magic. 通过直接推送到所需的目的地而不是让重新路由发挥作用,我已经达到了目标。 This works. 这可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM