属性 'status' 在类型 'Error' 上不存在
Property 'status' does not exist on type 'Error'
我正在使用 Angular 2 并将来自 HTTP 请求的错误传递给 logAndPassOn
方法。我收到错误 "Property 'status' does not exist on type 'Error'." The typeof Error is object 但似乎 typeof Error 会像 typeof File 一样被识别。
private logAndPassOn (error: Error) {
if (error.status == 403) {
Do things
}
return Observable.throw(error);
}
我通过将错误类型设置为 'any' 来解决这个问题,但似乎应该有更好的方法。是否有导入 typeof 错误的库?除了编写我自己的错误 class 并导入它之外,还有其他方法可以解决这个问题吗?
传递给 Http
错误回调的对象实际上是来自 @angular/http
模块的 Response
类型。这与您从初始 Http 调用中获得的 Response
相同。如果有错误响应,则 Response
将作为错误发送给订阅者。
import { Response } from '@angular/json';
http.get(..)
.subscribe(
(res: Response) => { doSomething(res.json()) }
(res: Response) => { ... });
所以基本上,您尝试使用 Error
是不正确的。它在您使用 any
时起作用,因为在运行时传递的是实际的 Response
对象, 是否具有 status
属性.
我正在使用 Angular 2 并将来自 HTTP 请求的错误传递给 logAndPassOn
方法。我收到错误 "Property 'status' does not exist on type 'Error'." The typeof Error is object 但似乎 typeof Error 会像 typeof File 一样被识别。
private logAndPassOn (error: Error) {
if (error.status == 403) {
Do things
}
return Observable.throw(error);
}
我通过将错误类型设置为 'any' 来解决这个问题,但似乎应该有更好的方法。是否有导入 typeof 错误的库?除了编写我自己的错误 class 并导入它之外,还有其他方法可以解决这个问题吗?
传递给 Http
错误回调的对象实际上是来自 @angular/http
模块的 Response
类型。这与您从初始 Http 调用中获得的 Response
相同。如果有错误响应,则 Response
将作为错误发送给订阅者。
import { Response } from '@angular/json';
http.get(..)
.subscribe(
(res: Response) => { doSomething(res.json()) }
(res: Response) => { ... });
所以基本上,您尝试使用 Error
是不正确的。它在您使用 any
时起作用,因为在运行时传递的是实际的 Response
对象, 是否具有 status
属性.