我在Angular 2应用程序中处理错误时遇到问题。后端服务器脱机时,控制台中出现此未捕获的错误:
我在服务中的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将此错误发送到控制台?
[不知道您要对HTTP请求使用哪种依赖关系,几乎不可能确定问题。
我最近在TypeScript中回答了有关HTTP请求的另一个问题:Http Request in TypeScript
希望这会有所帮助:)