[angular] Angular/RxJs When should I unsubscribe from `Subscription`

Based on : Using Class inheritance to hook to Angular 2 component lifecycle

Another generic approach:

_x000D_
_x000D_
export abstract class UnsubscribeOnDestroy implements OnDestroy {_x000D_
  protected d$: Subject<any>;_x000D_
_x000D_
  constructor() {_x000D_
    this.d$ = new Subject<void>();_x000D_
_x000D_
    const f = this.ngOnDestroy;_x000D_
    this.ngOnDestroy = () => {_x000D_
      f();_x000D_
      this.d$.next();_x000D_
      this.d$.complete();_x000D_
    };_x000D_
  }_x000D_
_x000D_
  public ngOnDestroy() {_x000D_
    // no-op_x000D_
  }_x000D_
_x000D_
}
_x000D_
_x000D_
_x000D_

And use :

_x000D_
_x000D_
@Component({_x000D_
    selector: 'my-comp',_x000D_
    template: ``_x000D_
})_x000D_
export class RsvpFormSaveComponent extends UnsubscribeOnDestroy implements OnInit {_x000D_
_x000D_
    constructor() {_x000D_
        super();_x000D_
    }_x000D_
_x000D_
    ngOnInit(): void {_x000D_
      Observable.of('bla')_x000D_
      .takeUntil(this.d$)_x000D_
      .subscribe(val => console.log(val));_x000D_
    }_x000D_
}
_x000D_
_x000D_
_x000D_

Examples related to angular

error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class error TS1086: An accessor cannot be declared in an ambient context in Angular 9 TS1086: An accessor cannot be declared in ambient context @angular/material/index.d.ts' is not a module Why powershell does not run Angular commands? error: This is probably not a problem with npm. There is likely additional logging output above Angular @ViewChild() error: Expected 2 arguments, but got 1 Schema validation failed with the following errors: Data path ".builders['app-shell']" should have required property 'class' Access blocked by CORS policy: Response to preflight request doesn't pass access control check origin 'http://localhost:4200' has been blocked by CORS policy in Angular7

Examples related to rxjs

Angular - "has no exported member 'Observable'" What is pipe() function in Angular How to convert Observable<any> to array[] TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable What is the difference between Subject and BehaviorSubject? Best way to import Observable from rxjs take(1) vs first() Observable Finally on Subscribe Getting an object array from an Angular service BehaviorSubject vs Observable?

Examples related to observable

Http post and get request in angular 6 Angular 4: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable How can I create an observable with a delay Return an empty Observable Angular/RxJs When should I unsubscribe from `Subscription` Using an array from Observable Object with ngFor and Async Pipe Angular 2 How to get data from observable in angular2 How to catch exception correctly from http.request()? How to create an Observable from static data similar to http one in Angular?

Examples related to subscription

Angular/RxJs When should I unsubscribe from `Subscription`

Examples related to angular-component-life-cycle

Angular/RxJs When should I unsubscribe from `Subscription`