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