Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get route data outside the router outlet

Is there a way to get the data params in a component that is outisde of router outlet.

const appRoutes: Routes = [
{ path: '', component: SitesComponent },
{ path: 'pollutants/newpollutant', component: PollutantComponent, data: { new: true, crumbs: ["New pollutant"] } }
];

Component

export class BreadcrumbsComponent implements OnInit {    
list: any[] = [] as any[];

constructor( private router: Router) {

}

ngOnInit() {
    this.router.events.subscribe((val) => {
        if (val instanceof NavigationEnd)
            console.log(val);
    });
}

}

Capturing navigation end as above only gives me url.

like image 833
americanslon Avatar asked Feb 02 '26 02:02

americanslon


1 Answers

Well, I am not sure if you can do this and in any case it doesn't sound like a good pattern. You can share data (state) through any component with a shared singleton service and observables.

And it doesn't have to be parent-child relationship https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

I hope that helped. It's basically what you are asking but not from the router service but a 'custom' shared service. Both of them use observables.

like image 194
DrNio Avatar answered Feb 03 '26 18:02

DrNio