My angular project is a non-standalone component and modules. In order to reduce the initial bundle size, @defer is one if the options.
I have been wondering if it is possible to use in non-standalone project. If it only works on standalone components, can you provide me the steps?
Steps I tried to do:
- Create a sample standalone project
- Imported
CommonModule
package - Used
@defer
in template component(html) - Run
npm run build
- Found no
lazy chunk files
after build.
My angular project is a non-standalone component and modules. In order to reduce the initial bundle size, @defer is one if the options.
I have been wondering if it is possible to use in non-standalone project. If it only works on standalone components, can you provide me the steps?
Steps I tried to do:
- Create a sample standalone project
- Imported
CommonModule
package - Used
@defer
in template component(html) - Run
npm run build
- Found no
lazy chunk files
after build.
1 Answer
Reset to default 0Only standalone components can lazy-loaded with a @defer
block.
https://angular.dev/guide/templates/defer#which-dependencies-are-deferred
In order for the dependencies within a
@defer
block to be deferred, they need to meet two conditions:
- They must be standalone. Non-standalone dependencies cannot be deferred and are still eagerly loaded, even if they are inside of
@defer
blocks.- They cannot be referenced outside of
@defer
blocks within the same file. If they are referenced outside the@defer
block or referenced within ViewChild queries, the dependencies will be eagerly loaded.