angular8 拦截器

首先  新建一个文件夹叫 interceptors,在该文件夹下建一个文件叫param.interceptor.ts,输入ng-http-interceptor创建

import { Injectable } from '@angular/core';
import {
    HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';
import { environment } from 'src/environments/environment';

@Injectable()
export class ParamInterceptor implements HttpInterceptor {
    intercept(req: HttpRequest<any>, next: HttpHandler) {
        const modifiedReq = req.clone({
            setParams:{ icode:environment.icode }
        })
        return next.handle(modifiedReq);
    }
}

在app.module.ts中添加

providers: [
      {
         provide:HTTP_INTERCEPTORS,
         useClass:ParamInterceptor,
         multi:true
      }
   ],

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页