MCPcopy
hub / github.com/angular/angular / loadChildren

Function loadChildren

packages/router/src/router_config_loader.ts:121–164  ·  view source on GitHub ↗
(
  route: Route,
  compiler: Compiler,
  parentInjector: Injector,
  onLoadEndListener?: (r: Route) => void,
)

Source from the content-addressed store, hash-verified

119 * an update to the extractor.
120 */
121export 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
166async function maybeResolveResources<T>(value: T): Promise<T> {
167 // 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

Used in the wild real call sites across dependent graphs

searching dependent graphs…