I have HeaderComponent which is common for all pages and appears on each page. so, I have created separate Component for Header and added in app.component.html using selector. I have created two child pages "Home" and "Second" and route it using <router-outlet>
in app.component.ts
Route
const routes: Routes = [{
path : "", component : HomeViewComponent
},{
path : "second/:id/:category", component : SecondComponent
}];
I am passing parameters id and category in second components.
in HeaderComponent I want to fetch this parameter when route changes so I did following code in header.component.ts
constructor(private router: Router, private route : ActivatedRoute) { }
ngOnInit() {
this.router.events.subscribe((val) => {
// see also
this.route.params.subscribe(params => {
console.log(params);
});
});
}
But, I am unable to get params. I am getting {} empty params in console I've attached stackblitz link as well for same.
https://stackblitz.com/edit/hardik?file=app%2Fapp.routing.module.ts
UPDATE: I've this header component in all pages not just even in second component. so I can't put it in second component only.