Angular 7 - 使用 queryParams 定义路由
Angular 7 - Define routes with queryParams
我想声明两条 Angular 路线:
- 对于特定项目 (
/items?id=SOME_ITEM_ID
)
- 所有项目的列表(
/items
)
由于以下方法无效,我想知道如何定义路线以满足我的需要?
export const routes: Routes = [
{ path: 'items?id=someItemId', component: ItemComponent },
{ path: 'items', component: AllItemsComponent },
];
我认为你不需要 queryParams 而需要 params,你应该像这样定义你的路线:
export const routes: Routes = [
{ path: 'items/:id', component: ItemComponent },
{ path: 'items', component: AllItemsComponent },
];
正如@cgTag 在评论中所说,queryParams 是透明的,您不需要在路由定义中声明它们。
我想声明两条 Angular 路线:
- 对于特定项目 (
/items?id=SOME_ITEM_ID
) - 所有项目的列表(
/items
)
由于以下方法无效,我想知道如何定义路线以满足我的需要?
export const routes: Routes = [
{ path: 'items?id=someItemId', component: ItemComponent },
{ path: 'items', component: AllItemsComponent },
];
我认为你不需要 queryParams 而需要 params,你应该像这样定义你的路线:
export const routes: Routes = [
{ path: 'items/:id', component: ItemComponent },
{ path: 'items', component: AllItemsComponent },
];
正如@cgTag 在评论中所说,queryParams 是透明的,您不需要在路由定义中声明它们。