【问题标题】:Angular 2 - Why my all routes are not redirected?Angular 2 - 为什么我的所有路线都没有重定向?
【发布时间】:2017-05-23 20:47:12
【问题描述】:
[{
    path: 'menu',
    component: MenuComponent
  },
  {
    path: 'how',
    component: HowItWorksComponent
  },
  {
    path: '',
    pathMatch: 'prefix',
    redirectTo: 'menu'
  }]

以上是我的根路由配置,为什么我的所有路由都没有重定向到菜单,因为空字符串应该是所有路由的前缀。 它在/how/menu 上显示各自的组件,并完美地重定向到/ 上的菜单,但它应该始终重定向到/menu,不是吗?

【问题讨论】:

    标签: angular redirect routing angular-routing angular-component-router


    【解决方案1】:

    你必须在/menu上设置useAsDefault

    [{
       path: 'menu',
       component: MenuComponent,
       useAsDefault: true,
    },
    ...
    

    【讨论】:

    • 我不明白这一点,我为什么要这样做?根据 victor 的帖子vsavkin.tumblr.com/post/146722301646/… 这应该始终挂载菜单组件
    • 事实上任何 404 也应该被重定向到菜单
    【解决方案2】:

    如果你想让你的最后一条路径捕捉到前两条没有的东西,那么你会想要:

    {
      path: '**',
      redirectTo: 'menu'
    }
    

    作为explained in the angular docs**是一个特殊的通配符路由

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      相关资源
      最近更新 更多