我想要一个带有那个 url 的路由
/collections/:collectionId/books/:bookId/:title
例如
/collections/10/books/456678/my-book.html
在我的 app-routing.module.ts 文件中
{path: 'collections/:collectionId/books/:bookId/:title', component: BookDetailsComponent},
是否可以不使用嵌套路由器 socket ?
我试过了
this.router.navigate(['/collections',{collectionId: 10, bookId: 456678, title: "my-book.html"}]);
但是出现了这样的错误
Cannot match any routes. URL Segment: 'collections;collectionId=10;bookId=456678;title=my-book.html'
如果我将参数作为数组传递,
this.router.navigate(['/collections', 10, 456678, "my-book.html"]);
我明白了
Error: Cannot match any routes. URL Segment: 'collections/10/456678/my-book.html
我设法在“collections”路由下使用“books”子路由实现了我想要的效果,但我想在没有嵌套路由的情况下实现。
谢谢
最佳答案
应该是
this.router.navigate(['/collections', '10', 'books', '456678', "my-book.html"]);
数字周围的引号不是必需的,但我认为这是很好的做法。
关于 Angular 2 路由器 : multiple route parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41013785/