Hello All我面临一些承诺和处理500内部服务器错误的问题我已经创建了一个Promises来处理post和获取服务但是当我收到500响应时我无法在烤面包机中显示它是我的代码。
var toasterService: ToasterService;
constructor(toasterService: ToasterService) {
this.toasterService = toasterService;
}
postService(url: string, bodyParam: any): Promise < any > {
return this.http
.post(url, bodyParam, this.options)
.toPromise()
.then()
.catch(this.handleError);
}
private handleError(error: any): Promise < any > {
let toast;
toast = {
type: 'info',
title: 'Data Zone',
body: 'Data Zone Failure!',
showCloseButton: boolean
};
this.toasterService.pop(toast);
return Promise.reject(error.message || error);
}
但是在这段代码中
this.toasterService.pop(toast);
这里是stacktrace
Uncaught (in promise): TypeError: Cannot read property 'toasterService'
of undefined
TypeError: Cannot read property 'toasterService' of undefined
它显示为未定义。我做错了什么,另一件事如何展示烤面包机以防止陷入困境。
答案 0 :(得分:0)
你不必为toastservice设置一个变量,带来toastservice&#39;在构造函数中足以使用&#39; this&#39;。
来引用它您还需要将http导入构造函数。
constructor(toasterService: ToasterService, http: HttpClient) { }
postService(url: string, bodyParam: any): Promise < any > {
return this.http
.post(url, bodyParam, this.options)
.toPromise()
.then()
.catch(this.handleError);
}
private handleError(error: any): Promise < any > {
let toast;
toast = {
type: 'info',
title: 'Data Zone',
body: 'Data Zone Failure!',
showCloseButton: boolean
};
this.toasterService.pop(toast);
return Promise.reject(error.message || error);
}