angular - 错误 TS2339:“可观察”类型上不存在属性“捕获”'
问题描述
无法添加 catch 运算符。它给出了“可观察”类型上不存在属性“捕获”的错误
[enter image description here][1]
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { empInterface } from './empInterface';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/catch';
@Injectable({
providedIn:'root'
})
export class DynamicempService {
private _url: string="/assets/data/employeeDb.json";
constructor(private localData: HttpClient) { }
getEmployee(): Observable<empInterface[]>{
return this.localData.get<empInterface[]>
(this._url).catch(this.errorMethod);
}
errorMethod(error: HttpErrorResponse){
return Observable.throw(error.message || "Server Error");
}
}
解决方案
Angular 6 使用 rxjs 版本 6 并且 catch 运算符已更改为 catchError 并且您可以像这样导入
import { map, filter, catchError, mergeMap } from 'rxjs/operators';
这就是如何通过管道使用运算符:
import { map } from 'rxjs/operators';
myObservable
.pipe(map(data => data * 2))
.subscribe(...);
推荐阅读
- android - 回收站中的 CardView 图像在平板电脑上滚动时放大太多
- c++ - 使用 lambda 初始化多维数组
- python - 如何在测验中添加下一个和上一个 [python]
- ssl - 访问 asmx 服务抛出错误无法为具有权限的 SSL/TLS 建立安全通道 - 虽然可以从 SOAPUI 工作
- performance - 提高 Haskell 中递归的效率
- parallel-processing - Julia 中的并行计算——在多核上运行一个简单的 for 循环
- javascript - 如何在 JavaScript 中处理 Object.prototype
- database - 在批处理模式下使用 Alembic 更改 sa.Column 可为空的属性
- python - Scipy Minimize - 无法最小化目标函数
- javascript - 调试石头剪刀布游戏?Javascript