0

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!!

Steve Miller
  • 300
  • 3
  • 11
  • Can you please specify what the deprecation notice is and on which line it occurs. – BizzyBob Mar 11 '21 at 21:40
  • subscribe is deprecated: Use an observer instead of a complete callback (deprecation) is what I see in Visual Studio Code... The lint complaint is on the .subscribe(() => ...) line. – Steve Miller Mar 11 '21 at 21:44
  • I don't get that in this [StackBlitz](https://stackblitz.com/edit/so-66590718?file=src/app/app.component.ts) with `rxjs 6.6.3`; – BizzyBob Mar 11 '21 at 22:00
  • [this github comment](https://github.com/ReactiveX/rxjs/issues/4159#issuecomment-791659997) indicates a possible TypeScript / VSCode issue. Says: "*I was able to get it to go away by click the version of typescript in the bottom right corner of vs code and then choosing the select typescript version option*". – BizzyBob Mar 11 '21 at 22:16
  • @BizzyBob, could you post the code snippet where you got it to work? That would be helpful! Thanks!! – Steve Miller Mar 12 '21 at 15:50
  • Sorry, I was not able to reproduce this problem. I just searched the issue and found that github issue that mentioned it. – BizzyBob Mar 12 '21 at 16:27

0 Answers0