Angular 7 - 使用 queryParams 定义路由

flo*_*olu 3 url typescript angular-routing angular angular7

我想声明两条 Angular 路线:

  1. 对于特定项目 ( /items?id=SOME_ITEM_ID)
  2. 对于所有项目的列表 ( /items)

由于以下不起作用,我想知道如何定义我的路线以满足我的需求?

export const routes: Routes = [
    { path: 'items?id=someItemId', component: ItemComponent },
    { path: 'items', component: AllItemsComponent },
];
Run Code Online (Sandbox Code Playgroud)

lov*_*s91 5

我认为你不需要 queryParams 而是 params,你应该像这样定义你的路线:

export const routes: Routes = [
    { path: 'items/:id', component: ItemComponent },
    { path: 'items', component: AllItemsComponent },
];
Run Code Online (Sandbox Code Playgroud)

正如@cgTag 在评论中所说,queryParams 是透明的,您不需要在路由定义中声明它们。