Angular HTTP响应错误处理应用程序
我有一个data.json文件,如下所示Angular HTTP响应错误处理应用程序,angular,typescript,angular6,Angular,Typescript,Angular6,我有一个data.json文件,如下所示 [ {"value":1}, {"value":2}, {"value":3}, {"value":3} ] &我正在使用Http获取数据。数据正常传输,但如果我的服务器关闭,它会抛出错误,我想向用户显示一些自定义消息,而不是那个错误。下面是我获取数据的函数 data: any; getData() { this.http.get('http://localhost/php/data.json').sub
[
{"value":1},
{"value":2},
{"value":3},
{"value":3}
]
&我正在使用Http获取数据。数据正常传输,但如果我的服务器关闭,它会抛出错误,我想向用户显示一些自定义消息,而不是那个错误。下面是我获取数据的函数
data: any;
getData() {
this.http.get('http://localhost/php/data.json').subscribe((res) => {
this.data = res;
console.log(this.data);
})
}
ngOnInit() {
this.getData();
}
subscribe接受错误处理回调作为第二个参数。您可以在此处查看API详细信息
您还可以使用rxjs提供的catchError操作符
import {catchError} from 'rxjs/operators'
this.http.get('http://localhost/php/data.json')
.pipe (
catchError((error) => // handle the error here; )
)
.subscribe((res) => {
this.data = res;
console.log(this.data);
})
如果要捕获特定实例,请执行以下操作:
getData() {
this.http.get('http://localhost/php/data.json').subscribe((res) => {
this.data = res;
console.log(this.data);
}, (err:HttpErrorResponse) => {
consdole.log(err)
})
}
我建议您使用拦截器来集中错误
处理:
http-intercepter.ts:
import { Injectable, Injector } from '@angular/core';
import {
HttpEvent,
HttpHeaders,
HttpInterceptor,
HttpResponse,
HttpErrorResponse,
HttpHandler,
HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(
private appService: AppService) {
}
/**
*
* @param req - parameter to handle http request
* @param next - parameter for http handler
*/
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const started = Date.now();
/**
* Handle newly created request with updated header (if given)
*/
return next.handle(req).do((event: HttpEvent<any>) => {
/**
* Sucessfull Http Response Time.
*/
if (event instanceof HttpResponse) {
const elapsed = Date.now() - started;
}
}, (err: any) => {
/**
* redirect to the error_handler route according to error status or error_code
* or show a modal
*/
if (err instanceof HttpErrorResponse) {
console.log(err);
}
});
}
}
找不到名称“HttpErrorResponse”此错误是正确的,但我建议用户。catch error:error=>{handleErrors;}因为您可以将代码错误处理到响应函数中使用import{HttpErrorResponse}from@angular/common/httpthis可以正常工作,但如果数据不来,http大约需要10秒,然后抛出错误。我不希望用户等待10秒
import { Injectable, Injector } from '@angular/core';
import {
HttpEvent,
HttpHeaders,
HttpInterceptor,
HttpResponse,
HttpErrorResponse,
HttpHandler,
HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(
private appService: AppService) {
}
/**
*
* @param req - parameter to handle http request
* @param next - parameter for http handler
*/
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const started = Date.now();
/**
* Handle newly created request with updated header (if given)
*/
return next.handle(req).do((event: HttpEvent<any>) => {
/**
* Sucessfull Http Response Time.
*/
if (event instanceof HttpResponse) {
const elapsed = Date.now() - started;
}
}, (err: any) => {
/**
* redirect to the error_handler route according to error status or error_code
* or show a modal
*/
if (err instanceof HttpErrorResponse) {
console.log(err);
}
});
}
}
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true,
}
]