Based on : Using Class inheritance to hook to Angular 2 component lifecycle
Another generic approach:
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_
And use :
@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_