首页 > 解决方案 > 如何使角度路由路径“post/:id”和“post/create”共存?

问题描述

我正在使用 Angular 9,这是我的路由部分。

const routes: Routes = [{
    path: '',component: PostComponent,
    children: [
      { path: '',redirectTo: '',pathMatch: 'full', },
      { path: 'post/:id', component: PostDetailComponent},
      { path: 'post/create', component: PostCreateComponent}
  ]
}];

当通过路由路径“/post/create”时,初始化的组件实际上是“post/:id”而不是“post/create”,

我该怎么做才能让他们一起工作?

标签: angularangular-routing

解决方案


路由是基于优先顺序的。所以上面定义的路由将在下面的路由之前匹配。尝试重新排序路由

const routes: Routes = [{
  path: '',component: PostComponent,
  children: [
    { path: '',redirectTo: '',pathMatch: 'full', },
    { path: 'post/create', component: PostCreateComponent},
    { path: 'post/:id', component: PostDetailComponent}
  ]
}];

推荐阅读