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

后端 未结 9 1643
醉话见心
醉话见心 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条回答
  •  Happy的楠姐
    2020-12-04 09:34

    Here's an example of the latest way to do it with conditional environment logic. Note that EventService and LogService are just examples of other services you may have).

    providers: [
        EventService,
        LogService,
        MyService,
        {
          provide: MyService,
          useClass: (environment.custom == null || environment.custom === '' || environment.custom === 'MyCustomerService')
            ? MyCustomService : MyService
        }
      ]
    ]
    

提交回复
热议问题