A set of directive and utilities to manage an angular zoneless app.
Using this library you can go completly zoneless and still support 3rd party libraries like @angular/material
.
This library will solve the following:
- Angular material components without zone.js
- Issue on angular material zoneless
- Issue on angular material zoneless
Warning: This package is experimental
Watch this video I made to fully understand the best way to utilize zoneless in your angular app.
npm i az-zoneless
In your AppModule
add the ZonelessModule
to the imports
array.
import { ZonelessModule } from 'az-zoneless'
@NgModule({
declarations: [
...
],
imports: [
...,
ZonelessModule
],
bootstrap: [AppComponent]
})
export class AppModule {
}
This library is meant to be used in an angular running in zoneless mode.
To make angular work without Zone.js
please do the following:
- from the
polyfills.ts
comment out the line whereZone.js
is imported
// import 'zone.js'; // Included with Angular CLI.
- in the
main.ts
you need to tell angular to not work withZone.js
platformBrowserDynamic()
.bootstrapModule(AppModule, {
ngZone: "noop",
})
.catch((err) => console.error(err));
Here is a lecture where I explain about angular in zoneless mode
- After using the library, make sure to not use the regular events:
<!-- Do not use the regular events -->
<button (click)="doSomething()"></button>
These events will still work but they are less performent.
- To improve performance and really take advantage of the fact that you are zoneless, please use the events like this:
<button (azClick)="doSomething()"></button>
- In case you do decide to run angular with
Zone.js
the library supplies you with an event that will run outside the angular zone.
<!-- the doSomething method will not trigger change detection and will run outside the angular zone -->
<button (azClick.zoneless)="doSomething()"></button>
If you are afraid to remove zone.js, we also supply with directives that will allow you to incrementally transition your app to be zoneless.
Instead of removing zone.js completly, you can run part of your component tree outside zone.js
With this directive you can run part of your component tree outside zone.js.
This will work only if you did not remove Zone.js
<div>
<h1>This part is inside zonejs</h1>
<button (click)="doSomething()">
clicking this will run change detection
</button>
<div *azZoneLess>
<h1>This part is outside zonejs</h1>
<button (click)="doSomething()">
clicking this will run outside the zone and will only update is you call
ChangeDetectorRef.detectChanges()
</button>
</div>
</div>
If you used the azZoneLess
you can go back to running in angular zone using the azZoneFull
directive.
This directive will only work if you did not remove Zone.js
.
<div>
<h1>This part is inside zonejs</h1>
<button (click)="doSomething()">
clicking this will run change detection
</button>
<div *azZoneLess>
<h1>This part is outside zonejs</h1>
<button (click)="doSomething()">
clicking this will run outside the zone and will only update is you call
ChangeDetectorRef.detectChanges()
</button>
<div *azZoneFull>
<!-- This will return us back to the zone.js -->
<button (click)="doSomething()">This runs in the zone.js</button>
</div>
</div>
</div>