简体   繁体   English

Angular 5提供基于环境的HTTP拦截器

[英]Angular 5 Provide http interceptor based on environment

I have the following two environments in my angular-cli (v1.5.1, angular v5) application: 我的angular-cli(v1.5.1,angular v5)应用程序中具有以下两个环境:

  1. dev 开发者
  2. prod 产品

Dev makes use of mock data, which I provide with an http-interceptor. 开发人员利用了模拟数据,该数据由http-interceptor提供。 Pro makes use of a live rest api. 专业版利用实时休息API。

How do I provide the http-interceptor on dev, but not on pro? 如何在dev上提供http-interceptor,在pro上不提供? I already tried the following, but it doesn't work: 我已经尝试了以下方法,但是不起作用:

{
  provide: HTTP_INTERCEPTORS,
  useFactory: () => {
    if (environment.useMockBackend === true) {
      return MockHttpInterceptor;
    }
    return false;
  },
  multi: true
}

In my Angular 5.2 project I used following approach. 在我的Angular 5.2项目中,我使用了以下方法。

app.module.ts app.module.ts

import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { environment } from '../environments/environment';
import { MyInterceptor } from './my.interceptor';

const commonProviders = [/*...*/];
const nonProductionProviders = [{ 
  provide: HTTP_INTERCEPTORS,
  useClass: MyInterceptor,
  multi: true
}];

@NgModule({
  imports: [
    HttpClientModule,
    // ...
  ],
  providers: [
    ...commonProviders,
    ...!environment.production ? nonProductionProviders : []
  ]
})

my.interceptor.ts 我的拦截器

import { Injectable } from '@angular/core';
import { HttpEvent, HttpRequest, HttpInterceptor, HttpHandler } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class MyInterceptor implements HttpInterceptor {
  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    // ...
    return next.handle(req);
  }
}

The idea is to export interceptor providers from environment file, prod environment exports do-nothing interceptor or just any other dummy provider (lets name it DefaultHttpInterceptor ) and dev exports MockHttpInterceptor . 这个想法是从环境文件中导出拦截器提供程序,prod环境导出无作用的拦截器或仅导出任何其他虚拟提供程序(将其命名为DefaultHttpInterceptor ),而dev导出MockHttpInterceptor

dev environment: export const INTERCEPTORS = {provide: HTTP_INTERCEPTORS, ... MockHttpInterceptor} 开发环境: export const INTERCEPTORS = {provide: HTTP_INTERCEPTORS, ... MockHttpInterceptor}

prod environment: export const INTERCEPTORS = {provide: HTTP_INTERCEPTORS, ... DefaultHttpInterceptor} 生产环境: export const INTERCEPTORS = {provide: HTTP_INTERCEPTORS, ... DefaultHttpInterceptor}

Then you can use it like usual: 然后您可以像平常一样使用它:

import { INTERCEPTORS } from './../environments/environment';
@NgModule({
providers      : [
        ...
        INTERCEPTORS 
        ...
    ]
...
})

I've come up with the following approach (this is in Angular 7), by drawing on the previous answers from @dhilt and @kemsky: 我通过借鉴@dhilt和@kemsky的先前答案,提出了以下方法(在Angular 7中):

Your dev environment file 您的开发环境文件

import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { MyDevInterceptor} from './my-dev.interceptor';

export const ENVIRONMENT_SPECIFIC_PROVIDERS = [
  { provide: HTTP_INTERCEPTORS, useClass: MyDevInterceptor, multi: true }
];

environment.prod.ts 环境产品

export const ENVIRONMENT_SPECIFIC_PROVIDERS = [];

app.module.ts app.module.ts

@NgModule({
  declarations: [],
  imports: [
    HttpClientModule
  ],
  providers: [
    ENVIRONMENT_SPECIFIC_PROVIDERS
  ]
})

It's simple, it works a treat, and it means that your code base contains no references to anything that's not required by your environment. 这很简单,可以很好地对待您,并且意味着您的代码库不包含对您的环境不需要的任何内容的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM