微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

简单高效!

原文地址:https://www.jb51.cc/angularjs/142016.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐