( route: Route, compiler: Compiler, parentInjector: Injector, onLoadEndListener?: (r: Route) => void, )
| 119 | * an update to the extractor. |
| 120 | */ |
| 121 | export async function loadChildren( |
| 122 | route: Route, |
| 123 | compiler: Compiler, |
| 124 | parentInjector: Injector, |
| 125 | onLoadEndListener?: (r: Route) => void, |
| 126 | ): Promise<LoadedRouterConfig> { |
| 127 | const loaded = await wrapIntoPromise( |
| 128 | runInInjectionContext(parentInjector, () => route.loadChildren!()), |
| 129 | ); |
| 130 | const t = await maybeResolveResources(maybeUnwrapDefaultExport(loaded)); |
| 131 | |
| 132 | let factoryOrRoutes: NgModuleFactory<any> | Routes; |
| 133 | if (t instanceof NgModuleFactory || Array.isArray(t)) { |
| 134 | factoryOrRoutes = t; |
| 135 | } else { |
| 136 | factoryOrRoutes = await compiler.compileModuleAsync(t); |
| 137 | } |
| 138 | |
| 139 | if (onLoadEndListener) { |
| 140 | onLoadEndListener(route); |
| 141 | } |
| 142 | // This injector comes from the `NgModuleRef` when lazy loading an `NgModule`. There is |
| 143 | // no injector associated with lazy loading a `Route` array. |
| 144 | let injector: EnvironmentInjector | undefined; |
| 145 | let rawRoutes: Route[]; |
| 146 | let requireStandaloneComponents = false; |
| 147 | let factory: NgModuleFactory<unknown> | undefined = undefined; |
| 148 | if (Array.isArray(factoryOrRoutes)) { |
| 149 | rawRoutes = factoryOrRoutes; |
| 150 | requireStandaloneComponents = true; |
| 151 | } else { |
| 152 | injector = factoryOrRoutes.create(parentInjector).injector; |
| 153 | factory = factoryOrRoutes; |
| 154 | // When loading a module that doesn't provide `RouterModule.forChild()` preloader |
| 155 | // will get stuck in an infinite loop. The child module's Injector will look to |
| 156 | // its parent `Injector` when it doesn't find any ROUTES so it will return routes |
| 157 | // for it's parent module instead. |
| 158 | rawRoutes = injector.get(ROUTES, [], {optional: true, self: true}).flat(); |
| 159 | } |
| 160 | const routes = rawRoutes.map(standardizeConfig); |
| 161 | (typeof ngDevMode === 'undefined' || ngDevMode) && |
| 162 | validateConfig(routes, route.path, requireStandaloneComponents); |
| 163 | return {routes, injector, factory}; |
| 164 | } |
| 165 | |
| 166 | async function maybeResolveResources<T>(value: T): Promise<T> { |
| 167 | // In JIT mode we usually resolve the resources of components on bootstrap, however |
no test coverage detected
searching dependent graphs…