Angular 4/5 – 路线paramMap vs params

前端之家收集整理的这篇文章主要介绍了Angular 4/5 – 路线paramMap vs params前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的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.

简单高效!

猜你在找的Angularjs相关文章