MCPcopy Create free account
hub / github.com/angular/angular / loadChildren

Function loadChildren

packages/router/src/router_config_loader.ts:126–172  ·  view source on GitHub ↗
(
  route: Route,
  compiler: Compiler,
  parentInjector: Injector,
  onLoadEndListener?: (r: Route) => void,
  // TODO(v23): Update `@angular/build` route extractor to pass `environmentInjector` and make it required.
  // g3-only environmentInjector?: EnvironmentInjector,
)

Source from the content-addressed store, hash-verified

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

Callers 1

loadChildrenMethod · 0.85

Calls 10

wrapIntoPromiseFunction · 0.90
runInInjectionContextFunction · 0.90
validateConfigFunction · 0.90
maybeResolveResourcesFunction · 0.85
maybeUnwrapDefaultExportFunction · 0.85
isArrayMethod · 0.80
mapMethod · 0.80
createMethod · 0.65
getMethod · 0.65
compileModuleAsyncMethod · 0.45

Tested by

no test coverage detected