1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 1.需要实现HttpInterceptor接口 @Injectable() export class SignInterceptor implements HttpInterceptor { intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>{ ... } }
2.暴露拦截器链 import { HTTP_INTERCEPTORS } from '@angular/common/http';
import {SignInterceptor} from './sign.interceptor';
//multi:true 表示有多个拦截器 export const httpInterceptorProviders = [ {provide: HTTP_INTERCEPTORS, useClass: SignInterceptor, multi: true}, ];
3.在模块中使用拦截器 @NgModule({ declarations: [...], imports: [...], providers: [httpInterceptorProviders] })
|