ActivatedRoute.queryParams - undefined

I am trying to capture the querystring parameter using Angular 2.0 and it is hard for me. I have such a url

http: // localhost: 5000 /? id_token = eyJ0eXAiO ....

And I want to be able to capture the id_token value before the routing holds, and I lose the parameter. I found this GitHub Issue Thread topic.

https://github.com/angular/angular/issues/9451

But it uses an obsolete method that no longer works. I tried the following methods

var foo = this.activatedRoute.queryParams.subscribe(params => { console.log(params); let id = params['id_token']; console.log(id); }); console.log(this.router.routerState.snapshot.root.queryParams["id_token"]); var bar = this.activatedRoute.params.subscribe( (param: any) => console.log(param['id_token'])); 

I tried these methods in the constructor as well as ngOnInit () of my component, but the console always displays undefined. I do it in the wrong place. I cannot change QS at all because it is created by Azure AD.

+5
source share
1 answer

Try typing ActivatedRoute and get queryParam using route.snapshot.queryParams['name'] as follows:

 class SomeComponent implements OnInit { constructor(private route: ActivatedRoute) { } ngOnInit(): void { let value = this.route.snapshot.queryParams['name']; } } 

Setting up queryParams through router.navigate works as follows:

 class SomeComponent { constructor(private router: Router) { } goSomewhere(): void { this.router.navigate(['somewhere'], { queryParams: { name: 'value' } }); } } 
+2
source

Source: https://habr.com/ru/post/1259105/


All Articles