Angular 2 路由器 : multiple route parameters

标签 angular

我想要一个带有那个 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/

相关文章:

javascript - 在最新的 TypeScript(可能是 v1.5)示例中,@(at 符号)是什么意思?

javascript - 从对象列表中删除项目 - Typescript

angular - 如何在 plunker 中正确添加 NPM 包?

使用异步等待的 Angular http get

linux - 如何从 Bash 脚本运行 npm 或 angular 2 ng serve 命令?

angular - ngFor:当循环位于 ng-container 中时,奇数/偶数行的颜色不同

javascript - 数据未定义或为空时如何使用 Lodash

angular - 当属性在 angular 4 中没有改变时如何强制更新模板

javascript - 也可以阻止在输入类型编号中输入负值的 Angular 方式(在复制粘贴和增加、减少时)

angular - 订阅一个 AngularFireObject