TIL, 2022-06-27, Actual Lazy Loading in a template
Lazy load Angular components
- ViewEngine adds all the necessary metadata to modules - but in Ivy, a Component can exist without a module.
- Ivy adds attributes like
type
,selectors
, and everything it needs at runtime. - Don’t use async/await when you compile to es2017. Zone js can not patch native async/await statements. Therefore you might run into trouble with Change Detection. If you compile your code to es2017 you should use a .then handler with a callback function.
- How to use Material/other components in the lazily-loaded component? Create a module inside it, declare
QuizCardComponent
, and import the modules we need. - Reacting on events of lazy-loaded components:
- The
EventEmitter
for the lazily loaded component will be subscribed to (since it’s aSubject
). - All lifecycle hooks get called, except
ngOnChanges
, since we manually update the input properties. To callngOnChanges
on the instance, we manually need to construct theSimpleChanges
object.
- The
-
Comments: Is there a way to load a module at runtime based on metadata?
ViewChild read
property:- Reference
- This is basically just to know if you will get the
ElementRef
or something else. - If you don’t provide the
read
parameter, VC will return theElementRef
instance if there is no component applied or the component instance if there is.
Understanding ViewChildren, ContentChildren, and QueryList in Angular
- The
@ViewChildren
decorator supports directive or component type as parameter, or the name of a template variable. read
property needed to make sure you get aViewContainerRef
.QueryList
is a fancy name for an object that stores a list of items.ViewChildren
don’t include elements that exist within theng-content
tag.ContentChildren
includes only elements that exists within theng-content
tag.
Angular Revisited: Tree-shakable components and optional NgModules
- In the current Angular generation, a component can and must only be declared in a single
NgModule
. Thedeclarables
that can be used are determined at compile time from the metadata of its declaring Angular module. - Transitive scopes:
- Transitive compilation scope: All the declarables that the Angular module can use in its template.
- Transitive exported scope: All the declarables in
exports
. It can also re-export other Angular modules by listing them in that same option.
@NgModule({
declarations: [HeroComponent, HeroListComponent],
imports: [CommonModule],
})
export class HeroModule {}
- This means that
HeroComponent
can referenceHeroListComponent
because it declares itself. And it can also useNgIf
since it is importingCommonModule
. - Reference suggests that components itself will have those
deps
, thus bypassing Angular module. -
Local component scope: Reference
- SCAM:
- When looking at a SCAM, we only have to consider a single component to determine whether an Angular module import is in use.
- Ex: A button with a cart and mat-button just uses
MatButton
andMatIcon
as its deps.
renderComponent
to render?