Angular默认预加载策略及自定义选择性预加载的加载时机咨询
Great questions about Angular's preloading strategies—let's break them down clearly:
1. Is Angular's default preloadingStrategy set to async?
Nope, Angular's default preloading strategy is actually NoPreloading (imported from @angular/router). This means lazy-loaded modules won't be preloaded by default; they'll only load when the user navigates to their associated route. The PreloadAllModules strategy (which preloads all lazy modules after initial load) is a common alternative, but it's not the out-of-the-box default.
2. Loading Timing of PostLoginModule with SelectiveStrategy
Looking at your code setup:
- The
/homeroute hasdata: { preload: true }attached, and you've setpreloadingStrategy: SelectiveStrategyinRouterModule.forRoot(). - Your
SelectiveStrategychecks ifroute.data.preloadistrue, and if so, calls theload()function to trigger the module load.
In this scenario, the PostLoginModule will be loaded asynchronously in the background after the initial page finishes loading. Here's the breakdown:
- Preloading strategies kick off only after the initial app bundle (and all eagerly loaded modules) have been loaded and the first page is rendered. The
load()function starts downloading the lazy module, but this happens in the background without blocking the user's interaction with the initial page. - This is still a form of lazy loading—just initiated automatically post-initial-load, rather than waiting for the user to click the
/homeroute.
Official Angular Documentation Clarification
Angular's official router docs do explicitly cover this behavior. The PreloadingStrategy interface documentation states:
Preloading strategies are executed after the initial navigation. They may load additional modules in the background, which speeds up subsequent navigations to routes in those modules.
For custom strategies like your SelectiveStrategy, this rule applies directly: the preload() method runs for every lazy route after the initial app load. When you return the result of load(), the module is fetched asynchronously in the background—exactly the behavior those blogs mention.
内容的提问来源于stack exchange,提问作者Nithin Kumar Biliya

