angular - 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();
}
}
解决方案
看来您正在使用 rxjs6。因此,您必须像这样使用finalize并pipe
与运算符一起使用:
import { finalize } from 'rxjs/operators';
logout() {
this.http.post('logout', {}).pipe(
finalize(() => {
this.app.authenticated = false;
this.router.navigateByUrl('/login');
})
).subscribe();
}
推荐阅读
- javascript - FabricJS剪辑路径边框颜色并将覆盖图像移动到剪辑路径后面?
- python - Selenium 无法通过 id 定位元素
- android - 当我将 LinearLayout 放入 ConstraintLayout 时不显示
- android - 是否可以查看哪个 bazel 目标传播 -lpthread 标志?
- javascript - 尝试将图像上传到 Firebase 存储时出错
- r - 改进 R 中的绘图
- c++ - 在 VSCode 和 Mingw 上编译 C++ Helloworld.cpp
- r - 重新排列两列,使相似的元素在一列中
- jquery - 防止 jQueryUI 可排序项目在排序完成之前移动和移动
- javascript - 为什么会出现这个错误?users.map 不是函数