首页 > 解决方案 > Angular 7 终于不起作用了

问题描述

我有一个与finally相关的问题,如何解决?我尝试像下面这样导入它,但它不起作用,然后我还检查了该解决方案: import 'rxjs/add/operator/finally'甚至import 'rxjs/operator'. 当我将鼠标悬停在 finally 上以查看问题是什么时,它会说'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();
  } 
}

标签: angular

解决方案


看来您正在使用 rxjs6。因此,您必须像这样使用finalizepipe与运算符一起使用:

import { finalize } from 'rxjs/operators';

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

推荐阅读