Reddi
Reddi

Reputation: 743

Angular 7 finally does not works

I have an issue related with finally, how to resolve it? I tried to import it like below, but it does not works, then I also checked that solution: import 'rxjs/add/operator/finally' or even import 'rxjs/operator'. When I hover on the finally to see what is the issue it says 'finally' does not exists on type 'Observable<Object>'. Any ideas how to solve it?

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AppService } from './app.service';
import { Router } from '@angular/router';
import { finally } from 'rxjs/operator';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  constructor(private app: AppService, private http: HttpClient, private router: Router) {
      this.app.authenticate(undefined, undefined);
  }

  logout() {
    this.http.post('logout', {}).finally(() => {
        this.app.authenticated = false;
        this.router.navigateByUrl('/login');
    }).subscribe();
  } 
}

Upvotes: 1

Views: 1704

Answers (2)

Ashish
Ashish

Reputation: 2068

We now use finalize instead of finally

import { finalize } from 'rxjs/operators'

observable().pipe( finalize());

Upvotes: 0

Johan Rin
Johan Rin

Reputation: 1950

It seems like you are using rxjs6. So you have to use finalize and use pipe with the operator like this:

import { finalize } from 'rxjs/operators';

logout() {
  this.http.post('logout', {}).pipe(
    finalize(() => {
      this.app.authenticated = false;
      this.router.navigateByUrl('/login');
    })
  ).subscribe();
}

Upvotes: 1

Related Questions