我在我的 Angular 5 应用程序中意外出现了 aw snap 错误页面。有什么解决办法吗?
我希望如果任何 api 返回 401 错误响应,用户就会自动注销。为此,我拦截每个请求,一旦错误代码出现 401,我就会清除本地存储中的 jwt 令牌,并且身份验证防护会阻止但是在实现拦截器之后(这方面的示例非常少,并且文档中也没有提及),我无法命中任何 HTTP 请求。下面是我的代码。提前致谢。
import { Injectable, Injector } from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse} from '@angular/common/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/observable/throw'
import 'rxjs/add/operator/catch';
@Injectable()
export class ResponseInterceptor implements HttpInterceptor {
constructor() {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => {
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
// do error handling here
console.log('and the error is ');
console.log(err)
}
});
} …Run Code Online (Sandbox Code Playgroud)