Angular 状态 0 如果 Api 离线
Angular Status 0 if Api offline
如果 API 在我的 angular 应用程序上离线,我会尝试显示一条消息。
在我的 HttpInterceptor 中,我有:
return next.handle(request).pipe(
tap(
event => {
if (event instanceof HttpResponse) {
// console.log('all looks good');
// http response status code
//console.log(event.status);
}
},
error => {
if (error.status == 0) {
error.message = 'API is Offline.', 'error'
}
console.error(error.status);
console.error(error.message);
}
)
);
所以我想在他失败时拦截return消息给我的组件或服务的错误。
我的组件调用服务:
this._service.new()
.subscribe(
res => {
},
err => {
this._messageHelperService.showToast(err.error.message, 'error');
}
);
有人可以帮助我吗?谢谢。
尝试在 HttpInterceptor 中使用 throwError
抛出错误。
示例:
tap(
event => {
if (event instanceof HttpResponse) {
// console.log('all looks good');
// http response status code
//console.log(event.status);
}
},
error => {
if (error.status === 0) {
return throwError('API is Offline., error')
// error.message = 'API is Offline.', 'error'
}
console.error(error.status);
console.error(error.message);
}
)
);
然后您可以按照您正在使用的方式访问组件中的错误。
如果 API 在我的 angular 应用程序上离线,我会尝试显示一条消息。
在我的 HttpInterceptor 中,我有:
return next.handle(request).pipe(
tap(
event => {
if (event instanceof HttpResponse) {
// console.log('all looks good');
// http response status code
//console.log(event.status);
}
},
error => {
if (error.status == 0) {
error.message = 'API is Offline.', 'error'
}
console.error(error.status);
console.error(error.message);
}
)
);
所以我想在他失败时拦截return消息给我的组件或服务的错误。
我的组件调用服务:
this._service.new()
.subscribe(
res => {
},
err => {
this._messageHelperService.showToast(err.error.message, 'error');
}
);
有人可以帮助我吗?谢谢。
尝试在 HttpInterceptor 中使用 throwError
抛出错误。
示例:
tap(
event => {
if (event instanceof HttpResponse) {
// console.log('all looks good');
// http response status code
//console.log(event.status);
}
},
error => {
if (error.status === 0) {
return throwError('API is Offline., error')
// error.message = 'API is Offline.', 'error'
}
console.error(error.status);
console.error(error.message);
}
)
);
然后您可以按照您正在使用的方式访问组件中的错误。