How to use ActivatedRoute in Angular 5?

前端 未结 5 513
-上瘾入骨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 19:45

    How to Get Route Parameters: The Angular Router provides two different methods to get route parameters:

    a. Using the route snapshot(ActivatedRoute),

    b. Using Router Observables

    ActivatedRoute in Angular: Provides access to information about a route associated with a component that is loaded in an outlet

    Step-1 Import the ActivatedRoute interface

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

    Step-2 Inject the ActivatedRoute in Constructor

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

    Step-3 To fetch a employee object by the given id and assign that object to its local employee property.

    ngOnInit() {
        this.employee = new Employee();
    this.id = this.route.snapshot.params['id']; 
    

    Note: Property Description

      snapshot: The current snapshot of this route
    

提交回复
热议问题