Angular routes with params and getting params

Viewed 10545

A user will be clicking a link in an email like this:

do-something/doSomething?thing=XXXXXXXXXXX

How can I define the route in the router and subscribe to get params?

Currently in router I have:

    {
     path: 'do-something/:id',
     component: DoSomethingComponent
    },

In the component:

    ngOnInit() {
     this.sub = this.route
      .params
      .subscribe(params => {
       console.log(params)
      });
    }

However, the route never matches. Shouldn't ":id" consider anything after reset-password as a param?

2 Answers

More than 1 hour wasted.

Both below did not work:
this.route.paramMap &
this.route.params

Only below worked:
this.route.queryParams

1.URL is like this:

http://localhost:4200/employee?tab=admin

2.Need to extract the value of query parameter tab, which is admin

3.Code that worked is:

ngOnInit() {
   this.route.queryParams.subscribe(params => {
    console.error(" ==== ", params["tab"]); // this will print `admin`
    // this.tabNameFromUrl = params["tab"];
  });
}

Hope that helps.

Related