router / Function

setupInjectionContextForLoadChildren

Workaround to emulate an injection context in the route's loadChildren resolver.

Notes

This enables asynchronous providers for routes, allowing dynamically provide injectables based on external factors.

The Injector used for the injection context is usually the nearest EnvironmentInjector.

This function does not transform child routes. Invoke on each route that needs an injection context.

See Also

Presentation

Returns

Route -

a new route config that can be used in replace of the given route config.

Parameters

NameTypeDescription
route
Route

the route config where a injection context is needed for loadChildren

Example usage

export const APP_ROUTES: Routes = [
 setupInjectionContextForLoadChildren({
   path: '',
   loadChildren: async (providerLoader = inject(MyProviderLoader)) => [
     {
       path: '',
       providers: await providerLoader.load(),
       children: [
         ...
       ],
     },
   ],
 }),
];