3

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 2

4

this one i got for angular site

import { Component, OnInit }  from '@angular/core';
import { ActivatedRoute }     from '@angular/router';
import { Observable }         from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Component({
  template:  `
    <p>Dashboard</p>
    <p>Session ID: {{ sessionId | async }}</p>
  `
})
export class AdminDashboardComponent implements OnInit {

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
   //read query param
    this.route
      .queryParamMap
      .map(params => console.log(params.get('thing')););

     //read param value 
      this.route
      .paramMap
      .map((params: ParamMap) => 
                           console.log(params.get('id')));
  }
}

Read Routinh here : https://angular.io/guide/router#!#optional-route-parameters


try like this

const appRoutes: Routes = [
  { path: 'do-something/:id', component: DoSomethingComponent, name: 'Details'}];

now navigation like

this.router.navigate( [
  'Details', { id: 'idvalue', param1: 'value1'
}]);//this you can try from code

this match

do-something/idvalue?param1=value1. //this try from browser

read query param like this

ngOnInit() {
  // Capture the access token and code
  this.route
      .queryParams
      .subscribe(params => {
          let thing = params['thing'];
      });
}

or try this

(new URL(location)).searchParams.get("parameter_name")
Sign up to request clarification or add additional context in comments.

4 Comments

what route have you defined in the router?
@Anthony - define route in route.ts file
When I subscribe to route and console.log(params) I only get {id: "idvalue"} not the other param values
@Anthony- thanks to you i am also trying at my end becuase i never tried this in my example ...
1

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.

2 Comments

http://localhost:4200/employee?id=13 => queryParams ----------------- http://localhost:4200/employee/13 => params
The question did start with query params URL. Thanks for the additional details, your details will help someone soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.