its*_*vid 7 try-catch observable typescript angular
以下代码:
private getJSON(): Observable<any> {
return this.http.get('./reportNav-NEW.json')
.map((res:any)=> res.json())
.catch((error:any) => console.log(error));
}
Run Code Online (Sandbox Code Playgroud)
错误给了我:
类型'(error:any)=> void'的参数不能赋给类型'(err:any,caught:Observable)=> ObservableInput <{}>'的参数.类型'void'不能分配给'ObservableInput <{}>'类型.
它发生在 .catch((error:any) => console.log(error));
its*_*vid 14
这对我有用(感谢评论中的@trevor)
private getJSON(): Observable<any> {
console.log(document.location.href);
return this.http.get('...')
.map((res:any)=> res.json())
.catch((error:any) => {
return Observable.throw(error);
})
}
Run Code Online (Sandbox Code Playgroud)
更新:现在使用rxjs 6,你必须从"rxjs /运算符"调用"throwError"运算符,并使用它而不是"Observable.throw".
| 归档时间: |
|
| 查看次数: |
20358 次 |
| 最近记录: |