How to use ActivatedRoute in Angular 5?

前端 未结 5 519
-上瘾入骨i
-上瘾入骨i 2020-12-16 19:26

I am trying to do exactly the same thing as in this post: Angular 4 get queryString

I am using Angular 5.2.5.

ActivatedRoute seems to be the thing to use to

5条回答
  •  忘掉有多难
    2020-12-16 20:00

    ActivatedRoute

    I'm late to the conversation but hope the following works for the future programmers who encounter the same issue.

    import the ActivatedRoute

    import { ActivatedRoute } from '@angular/router';
    

    Inject the dependency injection

    constructor(
        private route: ActivatedRoute,
      ) { }
    

    and to grab the id from the link you can use the following

      ngOnInit() {
      this.route.paramMap.subscribe(params => {
        this.product = products[+params.get('productId')];
      });
    }
    

提交回复
热议问题