简体   繁体   中英

conditionally add a library in and angular2 in app.module.ts

I want to conditionally add a library in and angular2 in my app.module.ts by ENV..if production add if not dont add. How? Below is what I want to do but does not work Eg

import { environment } from '../environments/environment';
import * as Raven from 'raven-js';
Raven
  .config(environment.sentry)
  .install();

export class RavenErrorHandler implements ErrorHandler {
  handleError(err:any) : void {
    Raven.captureException(err.originalError || err);
  }
}

@NgModule({
  declarations: [...]
  providers: [
       if (environment=='production'){
            { provide: ErrorHandler, useClass: RavenErrorHandler },
       }
  ],
  bootstrap: [AppComponent]
  })
  export class AppModule { }    

Use simple ternary operator.

providers: [
     { 
        provide: ErrorHandler, 
        useClass: environment=='production' ? RavenErrorHandler: ErrorHandler 
     }
],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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