首页 > 解决方案 > Angular-如何从 Angular 中的 URL 获取特定参数

问题描述

大家好,我有关于 URL 的问题。我想从下面给出的 URL 中获取 id ie:2。

https://jsonplaceholder.typicode.com/users/2

标签: angularurl

解决方案


您可以使用 ActivateRoute 从 url 获取参数。以下来自角度文档的示例:

 constructor(
    private route: ActivatedRoute,
    private router: Router  ) {}

  ngOnInit() {
    const heroId = this.route.snapshot.paramMap.get('id');
    this.hero$ = this.service.getHero(heroId);
  }

推荐阅读