Question presented to candidate: "Our Angular app has grown to forty screens and the first load is slow. Explain how the router decides what to show for a URL, and how lazy loading would help us here."
What a strong answer should cover:
- The router maps URL paths to components using a
Routesarray registered withprovideRouter(routes);<router-outlet>is where the matched component renders androuterLinknavigates without a page reload. - Matching is first match wins, in array order, with
**as a wildcard andredirectTofor redirects. - Lazy loading with
loadComponent(one standalone component) orloadChildren(a whole set of child routes) wraps a dynamicimport(), which the build turns into a separate chunk that downloads only when needed. - The loader runs on the first navigation to that route and is cached afterwards; the router emits
RouteConfigLoadStart/RouteConfigLoadEndaround it. - Concrete effect on the initial bundle, and the follow-up options: preloading, guards that run before the download (
canMatch), and@deferfor parts of a page.
Clarifying questions expected:
- "Are the screens standalone components already, or still grouped in NgModules?"
- "Is the slow part the JavaScript download, or data fetching after the screen appears?"
Code / implementation expected: Yes β a routes array with one eager route, one loadComponent and one loadChildren route.