americanslon
americanslon

Reputation: 4298

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.

Upvotes: 3

Views: 2606

Answers (1)

DrNio
DrNio

Reputation: 1976

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.

Upvotes: 1

Related Questions