我想在我的Angular 5应用程序中获取URL参数,我发现了两种方法:
1)使用paramMap@H_404_2@
ngOnInit() { this.hero$= this.route.paramMap .switchMap((params: ParamMap) => this.service.getHero(params.get('id'))); }
2)使用参数:@H_404_2@
ngOnInit() { this.sub = this.route.params.subscribe(params => { this.id = +params['id']; }); }
有什么区别吗?哪一个是最好的做法?@H_404_2@
根据文件:
Two older properties are still available. They are less capable than their replacements,discouraged,and may be deprecated in a future Angular version.@H_404_2@
params — An Observable that contains the required and optional parameters specific to the route. Use paramMap instead.@H_404_2@
简单高效!@H_404_2@