I have a main component that has a router-outlet in it. In the component that is loaded in the router-outlet I grab the url parameter like this:
ngOnInit():
ActivatedRoute: Contains the information about a route associated with a component loaded in an router outlet. If you would like to access route details outside of it, use the code below.
import { Component } from '@angular/core';
import { Router, ActivatedRoute, Params, RoutesRecognized } from '@angular/router';
export class AppComponent {
constructor(private route: ActivatedRoute, private router: Router) {
}
ngOnInit(): void {
this.router.events.subscribe(val => {
if (val instanceof RoutesRecognized) {
console.log(val.state.root.firstChild.params);
}
});
}
}
There are other ways to share data in between components for example by using a service.
For more details about how you can tackle this as concept, read comments here.