Javascript 'Observable<any>' 类型上不存在属性 'catch'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37073705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Property 'catch' does not exist on type 'Observable<any>'
提问by BrianRT
On the Angular 2 documentation page for using the Http service, there is an example.
在使用 Http 服务的 Angular 2 文档页面上,有一个示例。
getHeroes (): Observable<Stuff[]> {
return this.http.get(this.url)
.map(this.extractData)
.catch(this.handleError);
}
I cloned the angular2-webpack-starterproject and added the above code myself.
我克隆了angular2-webpack-starter项目并自己添加了上面的代码。
I imported Observable
using
我Observable
使用导入
import {Observable} from 'rxjs/Observable';
I'm assuming the properties Observable
are imported as well (.map
works). Looked at the changelog for rxjs.beta-6 and nothing is mentioned about catch
.
我假设属性Observable
也被导入(.map
有效)。查看了 rxjs.beta-6 的变更日志,没有提及catch
.
回答by Thierry Templier
Warning: This solution is deprecated since Angular 5.5, please refer to Trent's answer below
警告:此解决方案自 Angular 5.5 起已弃用,请参考下面 Trent 的回答
=====================
======================
Yes, you need to import the operator:
是的,您需要导入运算符:
import 'rxjs/add/operator/catch';
Or import Observable
this way:
或以Observable
这种方式导入:
import {Observable} from 'rxjs/Rx';
But in this case, you import all operators.
但在这种情况下,您导入所有运算符。
See this question for more details:
有关更多详细信息,请参阅此问题:
回答by Trent
With RxJS 5.5+, the catch
operator is now deprecated. You should now use the catchError
operator in conjunction with pipe
.
在 RxJS 5.5+ 中,该catch
运算符现已弃用。您现在应该将catchError
运算符与 结合使用pipe
。
RxJS v5.5.2 is the default dependency version for Angular 5.
RxJS v5.5.2 是 Angular 5 的默认依赖版本。
For each RxJS Operator you import, including catchError
you should now import from 'rxjs/operators' and use the pipe operator.
对于您导入的每个 RxJS 运算符,包括catchError
您现在应该从 'rxjs/operators' 导入并使用管道运算符。
Example of catching error for an Http request Observable
为 Http 请求 Observable 捕获错误的示例
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
...
export class ExampleClass {
constructor(private http: HttpClient) {
this.http.request(method, url, options).pipe(
catchError((err: HttpErrorResponse) => {
...
}
)
}
...
}
Notice here that catch
is replaced with catchError
and the pipe
operator is used to compose the operators in similar manner to what you're used to with dot-chaining.
请注意,这里catch
用catchError
和pipe
运算符替换了运算符,以类似于您习惯使用点链的方式组合运算符。
See the rxjs documentation on pipable(previously known as lettable) operators for more info.
回答by Prince Babbar
In angular 8:
for catch:
import { catchError } from 'rxjs/operators';
for throw:
import { Observable, throwError } from 'rxjs';
and code should be written like this.
getEmployees(): Observable<IEmployee[]> {
return this.http.get<IEmployee[]>(this.url).pipe(catchError(this.erroHandler));
}
erroHandler(error: HttpErrorResponse) {
return throwError(error.message || 'server Error');
}