How to inject different service based on certain build environment in Angular2?

后端 未结 9 1659
醉话见心
醉话见心 2020-12-04 08:50

I have HeroMockService that will return mocked data and HeroService that will call back end service to retrieve heroes from database.

Assum

9条回答
  •  醉酒成梦
    2020-12-04 09:44

    A great and simple solution is provided on this post by Luuk G.

    https://hackernoon.com/conditional-module-imports-in-angular-518294aa4cc

    In summary:

    let dev = [
    StoreDevtoolsModule.instrument({
        maxAge: 10,
      }),
    ];
    
    // if production clear dev imports and set to prod mode
    if (process.env.NODE_ENV === 'production') {
      dev = [];
      enableProdMode();
    }
    
    @NgModule({
      bootstrap: [
        Base,
      ],
      declarations: [
        Base,
      ],
      imports: [
        Home,
        RouterModule.forRoot(routes, { useHash: true }),
        StoreModule.forRoot(reducers.reducerToken),
        ...dev,
      ],
      providers: [
      ],
    })
    export class Root { }
    

提交回复
热议问题