TypeScript:类型'(错误:任何)=> void'的参数不能分配给类型的参数

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".

  • 什么id你不想抛出错误,而是处理它(使用模态等)然后忽略它? (4认同)

归档时间:

查看次数:

20358 次

最近记录:

6 年,11 月 前