为什么我的路由设置/:id和/ route在Angular中不起作用?

问题描述 投票:-1回答:2

在我的应用中,我有2条路线:

/employees/{id}
/employees/new

我尝试通过以下方式将组件附加到ROUTING阵列中的路由:

export const ROUTES: Routes = 
[...,
  {
   path: 'employees/:id',
   component: ViewEmployeeComponent
  },
  {
   path: 'employees/new',
   component: NewEmployeeComponent,
   pathMatch: 'full'
  }
]

view变体有效,但是当我尝试导航至employees/new时,未加载相应的组件,并且引发了404页面未找到异常。

如何在Angular中使用这两个路由路径?

angular routing
2个回答
1
投票

您只需要像波纹管那样交换两条路线

    {
      path: 'employees/new',
      component: NewEmployeeComponent,
      pathMatch: 'full'
    },
    {
        path: 'employees/:id',
        component: ViewEmployeeComponent
    }

也请记住这一点

路由在配置中的顺序很重要,这是通过设计。路由器在匹配时使用首个匹配获胜策略路线,因此应将更具体的路线放在不那么具体的上方路线。

您可以从他们的DOC中找到更多详细信息>


0
投票

您应该提供更多代码(例如,如何导航)

© www.soinside.com 2019 - 2024. All rights reserved.