属性“错误"在类型“"中不存在|无极" [英] Property 'error' does not exist on type '"" | Promise<any>'
问题描述
我正在尝试按照Angular 指南.
I'm trying to add some error handling to a service, following the Angular guide.
相关代码段:
private handleError (error: Response | any) {
// In a real world app, you might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
但是,我遇到TypeScript错误:
However, I'm getting a TypeScript error:
error TS2339: Property 'error' does not exist on type '"" | Promise<any>'.
Property 'error' does not exist on type '""'.
error TS2339: Property 'error' does not exist on type '"" | Promise<any>'.
Property 'error' does not exist on type '""'.
我能理解为什么发生的情况-error.json()
返回一个Promise<any>
,然后随后的body.error
行将不起作用,因为没有error
属性.但是,似乎应该期望从.json()
返回JSON对象.为什么会这样?我想念的不是Angular指南吗?
I can understand why it's happening-- error.json()
returns a Promise<any>
and then the subsequent line with body.error
wouldn't work because there is no error
property. However, it seems like it should expect a JSON object to be returned from .json()
. Why is that, and what am I missing that the Angular guide isn't?
推荐答案
同一件事刚发生在我身上.当您无法导入Response对象时,就会发生这种情况.
Same thing just happened to me. This happens when you fail to import the Response object.
import { Response } from '@angular/http';
这篇关于属性“错误"在类型“"中不存在|无极"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!