Skip to content

Commit 78910a3

Browse files
carolruoAndrewKushnir
authored andcommitted
docs: rework programmatically rendering components section to mention @defer (#61763)
PR Close #61763
1 parent ee4acc2 commit 78910a3

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

adev/src/content/guide/components/programmatic-rendering.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
TIP: This guide assumes you've already read the [Essentials Guide](essentials). Read that first if you're new to Angular.
44

5-
In addition to using a component directly in a template, you can also dynamically render components.
6-
There are two main ways to dynamically render a component: in a template with `NgComponentOutlet`,
7-
or in your TypeScript code with `ViewContainerRef`.
5+
In addition to using a component directly in a template, you can also dynamically render components
6+
programmatically. This is helpful for situations when a component is unknown initially (thus can not
7+
be referenced in a template directly) and it depends on some conditions.
8+
9+
There are two main ways to render a component programmatically: in a template using `NgComponentOutlet`,
10+
or in your TypeScript code using `ViewContainerRef`.
11+
12+
HELPFUL: for lazy-loading use-cases (for example if you want to delay loading of a heavy component), consider
13+
using the built-in [`@defer` feature](/guide/templates/defer) instead. The `@defer` feature allows the code
14+
of any components, directives, and pipes inside the `@defer` block to be extracted into separate JavaScript
15+
chunks automatically and loaded only when necessary, based on the configured triggers.
816

917
## Using NgComponentOutlet
1018

@@ -95,9 +103,11 @@ In the example above, clicking the "Load content" button results in the followin
95103

96104
## Lazy-loading components
97105

98-
You can use both of the approaches described above, `NgComponentOutlet` and `ViewContainerRef`, to
99-
render components that are lazy-loaded with a standard
100-
JavaScript [dynamic import](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import).
106+
HELPFUL: if you want to lazy-load some components, you may consider using the built-in [`@defer` feature](/guide/templates/defer)
107+
instead.
108+
109+
If your use-case is not covered by the `@defer` feature, you can use either `NgComponentOutlet` or
110+
`ViewContainerRef` with a standard JavaScript [dynamic import](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import).
101111

102112
```angular-ts
103113
@Component({

packages/core/src/render3/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {assertComponentDef} from './errors';
6060
* applicationRef.attachView(componentRef.hostView);
6161
* componentRef.changeDetectorRef.detectChanges();
6262
* ```
63-
*
63+
*
6464
* @param component Component class reference.
6565
* @param options Set of options to use:
6666
* * `environmentInjector`: An `EnvironmentInjector` instance to be used for the component.

0 commit comments

Comments
 (0)