In this example, I have shared how to get query params from url in Angular 2? You can do by injecting an instance of ActivatedRoute one can subscribe to a variety of observables, including a queryParams and a params observable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import {Router, ActivatedRoute, Params} from '@angular/router'; import {OnInit, Component} from '@angular/core'; @Component({...}) export class MyComponent implements OnInit { constructor(private activatedRoute: ActivatedRoute) {} ngOnInit() { // Note: Below 'queryParams' can be replaced with 'params' depending on your requirements this.activatedRoute.queryParams.subscribe(params => { const userId = params['userId']; console.log(userId); }); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.