TIL, 2022-07-15, Angular exportAs
Understand exportAs In Angular Directive Angular
- Template variables are defined in HTML using
#
. They can be used to get a reference to a component and can be used to get a reference of the variable in the directive class usingViewChild
, or it can be passed to another component as anInput
. - For directives, we can use
exportAs
to assign the directive to a template variable with#myTemp=myDirective
.
Take Advantage of the exportAs Property in Angular
- You use
exportAs
to get access to the component’s methods via a template variable.
Get reference to a directive used in a component
- Access via constructor.
@Component({
})
export class MyComponent implements OnInit {
constructor(private directive: MyDirective) { }
ngOnInit(): void {
assert.notEqual(this.directive, null); // it passes!
}
}
5 Methods to Reduce JavaScript Bundle Size
- Code-split with Webpack via
lazy
. - Webpack plugins for tree shaking:
babel-plugin-lodash
for Babel andbabel-plugin-import
. - Webpack bundle analyzer.
- Compressing via Brotli or gzip.
- Use production mode.