Angular multiple modules providing same service

549 Views Asked by At

I am using a library (FontAwesome for Angular) which provides two services under the same name:

Lets say the first one is the real one:

@Injectable({
  providedIn: 'root',
})
export class Service { }

@NgModule({
  exports: [...],
})
export class Module {}

and the second one provides a mock version

@Injectable({
  providedIn: 'root',
})
export class MockService implements Service { }

@NgModule({
  exports: [...],
  provide: [{ provide: Service, useExisting: MockService }],
})
export class MockModule {}

Normally you would either use Module or MockModule. I now ran into a case where I would like to use both of them (arguably not very smart but it is in some util testing modules to reduce boilerplate code).

Is it possible to import Module and MockModule but having access to the actual Service and not the mock implementation MockService? So far I played around with the order of the modules but any order results in the MockService being injected.

Would a change in the services from providedIn: 'root' to instead provide them in the module directly make this possible or is there any other way to get access to the real service again?

0

There are 0 best solutions below