Currently, I am in the process of developing a library and sample application using Angular 11. One specific requirement is to enable the consuming application to inject an implementation of a base class into the library. Within the library, I have already established default logic. In order to define a provider in the consuming app, I have structured it as follows:
// app.module.ts within the main application
providers: [
{
provide: 'MyServiceBase',
useClass: DefaultMyService
}
]
The base class itself is defined in the Library like so:
export abstract class MyServiceBase{
abstract myFunction();
}
Furthermore, the default logic is implemented within the library as well:
@Injectable({
providedIn: 'root',
})
export class DefaultMyService extends MyServiceBase {
...
}
To make use of this service within a component in the library, I inject it accordingly:
constructor(@Inject('MyServiceBase') private myService: MyServiceBase) {
...
}
However, a challenge arises when I notice that the instance of DefaultMyService injected into the consuming app differs from that in the library. While debugging the application, I observe that the code utilized in the consuming app refers to JavaScript exported by the library, whereas the code in the library itself is actually TypeScript.
I am curious if there exists a solution to ensure that the same instance is injected into both the library and consuming app?