本文介绍了如何在Angular2中处理net :: ERR_CONNECTION_REFUSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误处理显示如何处理错误,如下所示:
error-handling shows how to handle errors as follows:
private handleError (error: Response | any) {
// In a real world app, we 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 Promise.reject(errMsg);
}
我想访问API服务器,但服务器尚未启动.然后我得到了错误:
I'd like to access an API server, but the server hasn't started. Then I got the error:
http://localhost:3000/api/heroes net::ERR_CONNECTION_REFUSED
我需要礼貌地告诉用户API服务器尚未启动.我应该如何处理错误?
I need to tell the user politely that the API server hasn't started. How should I handle the error?
错误响应为:
_body:ProgressEvent
headers:Headers
ok:false
status:0
statusText:""
type:3
url:null
我可以根据回复的状态来处理此问题吗?
Could I handle this according to the status of the response?
推荐答案
//First inject the router in the constructor
private handleError (error: Response | any) {
//Your other codes
if (error.status == 0){ //or whatever condition you like to put
this.router.navigate(['/error']);
}
}
这篇关于如何在Angular2中处理net :: ERR_CONNECTION_REFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!