【发布时间】:2017-11-03 11:35:39
【问题描述】:
我的 Angular 2 应用程序中的错误处理存在问题。 当后端服务器离线时,我在控制台中收到了这个未捕获的错误:
获取 http://localhost:4200/api/public/index.php/data 504(网关超时)
我在服务中的 http.get 是这样的:
getData(): Observable<any> {
let apiUrl = `${API_URL}data`;
this.http.get(apiUrl)
.map((res => res.json() || []))
.subscribe(response => {
let data = [];
let index = 0;
if (typeof(response.data) !== 'undefined' && response.success !== false) {
// add index to each entry
response.data.forEach(entry => {
data.push({
id: index++,
title: entry.title,
others: entry.others
});
});
this.dataCollection = data;
this.subject.next(data);
}
}, error => {
this.subject.error("The Server could not be reached. Please wait until a connection can be established, or reload the page.");
});
return this.subject.asObservable();
}
如何防止 Angular 2 将此错误发送到控制台?
【问题讨论】:
-
我不认为你可以;即使您的应用程序没有错误地处理它,浏览器“知道”请求已失败。
标签: javascript php angular typescript angular2-http