Search code examples
angulartypescripturlangular-routingangular7

Angular 7 - Define routes with queryParams


I want to declare two Angular routes:

  1. For a specific item (/items?id=SOME_ITEM_ID)
  2. For a list of all items (/items)

As the following isn't working, I want to know how I can define my routes to suite my needs?

export const routes: Routes = [
    { path: 'items?id=someItemId', component: ItemComponent },
    { path: 'items', component: AllItemsComponent },
];

Solution

  • I think you don't want queryParams but params, you should define your route like this :

    export const routes: Routes = [
        { path: 'items/:id', component: ItemComponent },
        { path: 'items', component: AllItemsComponent },
    ];
    

    As @cgTag says in comment, queryParams are transparent and you don't need to declare them in route definition.