Search code examples
angularangular-routingangular-resolver

make an angular global resolver to make data available in every route


I am trying to make a global resolver which will provide every route... Something like this

this.activatedRoute.data =>

{
    global: {...}
    route: {...}
}

This is what i am trying

const routes: Routes = [
    {
        path: '',
        resolve: { globalResolve: globalResolverService },
        children: [
            { path: '', component: HomeComponent, resolve: { home: ViewHomeResolver } },
            { path: 'getting-started', component: GettingStartedComponent },
            { path: 'expert/:slug', component: ProfileComponent, resolve: { profile: ProfileResolver } },
            { path: 'loan-types', component: Loantype2Component, resolve: { grid: Loan2Resolver } },
            { path: 'loan-types/:loan', component: LoanCategoryDetailsComponent, resolve: { loanDetails: LoanDetailsResolver } },

        ]},
];

The above results in the desired outcome only for homeComponent.


Solution

  • Your this.activatedRoute.data means you're accessing its own route data scope,

    To access the parent data, use this.activatedRoute.parent.data