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, 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
      }
   ],

 

你可能感兴趣的:(angular8)