Angular 7最终不起作用

时间:2019-01-26 12:03:25

标签: angular

我最后有一个问题,该如何解决?我试图像下面那样导入它,但是它不起作用,然后我还检查了该解决方案: import 'rxjs/add/operator/finally'甚至import 'rxjs/operator'。当我徘徊在最后看问题是什么时,它说'finally' does not exists on type 'Observable<Object>'.有解决问题的想法吗?

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();
  } 
}

2 个答案:

答案 0 :(得分:1)

似乎您正在使用rxjs6。因此,您必须使用finalize并将pipe与像这样的运算符一起使用:

import { finalize } from 'rxjs/operators';

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

答案 1 :(得分:0)

我们现在使用finalize代替finally

import { finalize } from 'rxjs/operators'

observable().pipe( finalize());