az-zoneless
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

az-zoneless

Known Vulnerabilities build codecov npm version

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:

Warning: This package is experimental

Installation

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 {
}

Set your app to be zoneless

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:

  1. from the polyfills.ts comment out the line where Zone.js is imported
// import 'zone.js';  // Included with Angular CLI.
  1. in the main.ts you need to tell angular to not work with Zone.js
platformBrowserDynamic()
  .bootstrapModule(AppModule, {
    ngZone: "noop",
  })
  .catch((err) => console.error(err));

Lecture about using angular in zoneless

Here is a lecture where I explain about angular in zoneless mode

Events

  1. 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.

  1. To improve performance and really take advantage of the fact that you are zoneless, please use the events like this:
<button (azClick)="doSomething()"></button>
  1. 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>

Directives

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

azZoneLess

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>

azZoneFull

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>

Readme

Keywords

none

Package Sidebar

Install

npm i az-zoneless

Weekly Downloads

5

Version

0.0.7

License

none

Unpacked Size

128 kB

Total Files

26

Last publish

Collaborators

  • ywarezk