如何使用ID为String的路由参数?
{path: 'param/:id', component: MyComponent}
let id = this.route.snapshot.params['id'];
Blockquote
最佳答案
如您所示,使用路由快照获取 id
查询参数应该会给您一个字符串。如果您想将该字符串隐式转换为数字,以便在导航的路由参数中使用它,您可以在变量前加上 +
前缀:
// (+) converts string 'id' to a number
let id = +this.route.snapshot.params['id'];
然后:
this.router.navigate(['/example', id]);
查看文档中获取查询参数的示例 https://angular.io/guide/router#snapshot-the-no-observable-alternative .
关于node.js - 在 ID 为 String 的 Angular 4 中使用路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603142/