I was trying to find the difference between .snapshot and .queryParam in angular for the same i created a demo with the component code as follows
import { Component, OnInit } from '@angular/core';
import{ActivatedRoute,ParamMap, Router} from '@angular/router'
@Component({
selector: 'app-routing-eg',
templateUrl: './routing-eg.component.html',
styleUrls: ['./routing-eg.component.css']
})
export class RoutingEgComponent implements OnInit {
constructor(private _route : ActivatedRoute,private router:Router) { }
ngOnInit(): void {
const id = this._route.snapshot.paramMap.get('id');
let par:any;
this._route.queryParams.subscribe((params) =>{
par = params['id'];
})
console.log('id' + id)
console.log('par' + par)
this.router.navigate(['routed/:2'])
console.log('id 2 ' + id)
console.log('par 2' + par)
}
}
the output that i get for the above code is
id1
parundefined
id 2 1
par 2undefined
Where would i be going wrong that i recieve undefined when i use subscribe