如果你想有一个共享的拦截器,你有角度
HTTP_INTERCEPTOR
:
module.ts
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: LoginInterceptor,
multi: true
}
]
})
以及服务
LoginInterceptor
@Injectable()
export class LoginInterceptor implements HttpInterceptor {
constructor() {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap(() => {
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
// DO here what you want
// this.router.navigate(['login']);
}
}
)
);
}
}
另一种选择
警卫
在你的路线顶端。