I am using angular 11, rxjs v6.6, and typescript 4.0.5. There are 4 subscribe methods of Subscription of which 3 are deprecated. I have searched the ends of the google earth to find syntax that will work for the one subscribe methods that is not deprecated. I simply cannot find any code snippet that works. I do not want to disable the 'deprecation' rule in tslint nor do I want to disable the rule in the component file I am working on.
It would be fantastic if the Rxjs authors, whose documentation is fairly rich, would provide explicit examples of how to subscribe to an event...
This is my code:
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit, OnDestroy {
subscription: Subscription;
constructor(private router: Router) {
}
ngOnInit() {
this.subscription = this.router.events
.pipe(
filter((e): e is NavigationEnd => e instanceof NavigationEnd)
)
.subscribe(() => { window.scrollTo(0, 0) });
}
ngOnDestroy() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}
If anyone can provide a working example, I would be extremely grateful!!