ngFor
to loop the array in patterns, refer to the trackBy
function, which will return a unique identifier for each element.trackBy
function, Angular will understand which element has changed, and then make changes to the DOM only for that particular element. <li *ngFor="let item of items;">{{ item }}</li>
// <li *ngFor="let item of items; trackBy: trackByFn">{{ item }}</li> // trackByFn(index, item) { return item.id; // unique id corresponding to the item }
const
if they are not assigned again. let car = 'ludicrous car'; let myCar = `My ${car}`; let yourCar = `Your ${car}`; if (iHaveMoreThanOneCar) { myCar = `${myCar}s`; } if (youHaveMoreThanOneCar) { yourCar = `${youCar}s`; }
// car , (const) const car = 'ludicrous car'; let myCar = `My ${car}`; let yourCar = `Your ${car}`; if (iHaveMoreThanOneCar) { myCar = `${myCar}s`; } if (youHaveMoreThanOneCar) { yourCar = `${youCar}s`; }
RxJs
operators. import 'rxjs/add/operator/map'; import 'rxjs/add/operator/take'; iAmAnObservable .map(value => value.item) .take(1);
import { map, take } from 'rxjs/operators'; iAmAnObservable .pipe( map(value => value.item), take(1) );
async
channels are automatically unsubscribed, simplifying the code and eliminating the need for manual subscription management. It also reduces the risk of accidental unsubscribe in the component, resulting in a memory leak. This is fixable using the lint rule for detecting unsigned observable objects. // <p>{{ textToDisplay }}</p> // iAmAnObservable .pipe( map(value => value.item), takeUntil(this._destroyed$) ) .subscribe(item => this.textToDisplay = item);
// <p>{{ textToDisplay$ | async }}</p> // this.textToDisplay$ = iAmAnObservable .pipe( map(value => value.item) );
take
, takeUntil
, etc.lint
rule to detect observable objects from which there is no reply. iAmAnObservable .pipe( map(value => value.item) ) .subscribe(item => this.textToDisplay = item);
private _destroyed$ = new Subject(); public ngOnInit (): void { iAmAnObservable .pipe( map(value => value.item) // iAmAnObservable , / takeUntil(this._destroyed$) ) .subscribe(item => this.textToDisplay = item); } public ngOnDestroy (): void { this._destroyed$.next(); this._destroyed$.complete(); }
takeUntil
to listen for changes until another watched object returns the value: iAmAnObservable .pipe( map(value => value.item), take(1), takeUntil(this._destroyed$) ) .subscribe(item => this.textToDisplay = item);
takeUntil
together with take
in the example above. This avoids memory leaks caused by the fact that no value was assigned to the subscription before the component was removed.takeUntil
subscription will still be suspended, as it were, until it receives its first value, but since the component has already been deleted, it will never receive a value, and this will lead to a memory leak.Angular
application only when they are used. // app.routing.ts { path: 'not-lazy-loaded', component: NotLazyLoadedComponent }
// app.routing.ts { path: 'lazy-load', loadChildren: 'lazy-load.module#LazyLoadModule' } // lazy-load.module.ts import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterModule } from '@angular/router'; import { LazyLoadComponent } from './lazy-load.component'; @NgModule({ imports: [ CommonModule, RouterModule.forChild([ { path: '', component: LazyLoadComponent } ]) ], declarations: [ LazyLoadComponent ] }) export class LazyModule {}
Source: https://habr.com/ru/post/447042/
All Articles