小编Rah*_*mov的帖子

如何在 Angular 中使用快照或 ActivatedRoute 订阅者从 URL 获取 id?

我使用快照方法获取价值,但在这种情况下它在“类”之后获取价值 20 但我需要 33 路径,如获取

credits/33/classes/20 only 20 or credits/33/classes/ only null("")
Run Code Online (Sandbox Code Playgroud)

更新:我找到了我的问题的解决方案。

现在它正在正确获取 id。错误是访问了右子组件中的元素,在 Snapshot 版本中的子组件 MatDialog 组件中不起作用。

constructor(private route: ActivatedRoute) {} 
 ngOnInit(): void {
   console.log(parseInt(this.route.snapshot.paramMap.get('id1')));
Run Code Online (Sandbox Code Playgroud)

如果您的 url 有 2 个 Id 值,则可以使用 route.parent 的快照

   console.log(parseInt(this.route.parent.snapshot.paramMap.get('id1')));
}
Run Code Online (Sandbox Code Playgroud)

angular angular-router angular-activatedroute

3
推荐指数
1
解决办法
2万
查看次数