I working with Angular 8.
This question is only for Angular DI experts.
At first, I made an Angular library that will be reused in many project.
In this Library, I made an abstract service, that have to be defined in the project using it.
I defined an abstract class in my Library named BridgeService
and In my main project, I create my BridgeService implementing it: ( in the next lines, I will show you the abstract BridgeService)
MainProject > BridgeService :
import { BridgeService as AbstractBridgeService } from 'myLibrary';
@Injectable()
export class BridgeService implements AbstractBridgeService {
hello(){
alert('hello Word');
}
}
MainProject > UsersModule :
import { UsersModule as LibraryUsersModule } from 'myLibrary';
@NgModule({
imports: [
CommonModule,
LibraryUsersModule,
],
//...
providers: [
BridgeService
// some try:
//{ provide: BridgeService, useClass: BridgeService }
],
})
export class UsersModule {
constructor(private bridgeService: BridgeService){
this.bridgeService.hello();
}
}
And now we have to see the library concerned files:
Library > bridge.service.ts:
export abstract class BridgeService {
abstract hello(): void;
}
This BridgeService have to be defined in UsersModule
Library > UsersModule:
@NgModule({
//...
providers: [
// My Try:
{ provide: BridgeService, useValue: BridgeService}
],
})
export class UsersModule {
# my goal is that I can uncomment this piece of code:
//constructor(private bridgeService: BridgeService){
// this.bridgeService.hello();
//}
}
I want that you touch just two lines in this code:
1) In library's UsersModule providers array: the line of BridgeService that I want to declare as an abstract to be generic.
2) In project's UsersModule providers array: the line of BridgeService that I want to define to take place of the abstract in the Library UserModule.