Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Router: multiple route parameters

Tags:

angular

I want to have a route with that url

/collections/:collectionId/books/:bookId/:title

e.g.

/collections/10/books/456678/my-book.html

in my app-routing.module.ts file

{path: 'collections/:collectionId/books/:bookId/:title', component: BookDetailsComponent},

Is is possible without using nested router outlets?

I tried

this.router.navigate(['/collections',{collectionId: 10, bookId: 456678, title: "my-book.html"}]);

but got an error like

Cannot match any routes. URL Segment: 'collections;collectionId=10;bookId=456678;title=my-book.html'

if I pass the paramaters as an array instead,

this.router.navigate(['/collections', 10, 456678, "my-book.html"]);

I get

Error: Cannot match any routes. URL Segment: 'collections/10/456678/my-book.html

I managed to achieve what I wanted using a 'books' child route under the 'collections' route, but I'd like to do it without nested routes.

Thanks

like image 981
David Avatar asked Dec 15 '22 02:12

David


1 Answers

It should be

this.router.navigate(['/collections', '10', 'books', '456678', "my-book.html"]);

The quotes around the numbers are not necessary but I think it's good practice.

like image 95
Günter Zöchbauer Avatar answered Dec 28 '22 10:12

Günter Zöchbauer