angular-plugin-architecture icon indicating copy to clipboard operation
angular-plugin-architecture copied to clipboard

Shared InjectionToken in a shared singleton service issue

Open parker-mike opened this issue 4 years ago • 1 comments

Using share-lib-between-app-and-plugins branch, when defining an InjectionToken that injected into a shared service and provided in the AppModule's NgModule.providers is not injected into the service called from within the plugins throwing

ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[InjectionToken test]: ... NullInjectorError: No provider for InjectionToken test!

shared:

export const APP_TEST = new InjectionToken<TestValue>("test");
@Injectable({
  providedIn: 'root'
})
export class TestService
{
  constructor(@Inject(APP_TEST) private tv: TestValue...
...
  test() { return this.tv; }
}

AppModule:

providers: [
...
{provide: TestValue, useValue: ...}]
...

AppComponent:

export class AppComponent implements OnInit {
  constructor(
    private injector: Injector,
    private pluginLoader: PluginLoaderService,
    private ts: TestService) {}
  ngOnInit() {
    console.log(this.ts.test()); // <-- Works as expected
...

Plugin 1:

export class Plugin1Component implements OnInit {
  x = false;
  constructor(private ts: TestService) {}
  ngOnInit() {
    console.log(this.ts.test());  // <-- Throws
...

It looks like plugin's injector is trying to create a new instance of the service and when trying to resolve the dependency for the injected token it thinks that it's another token, not the one that was provided in the AppModule... Any ideas how this can be resolved will be very welcomed.

parker-mike avatar Jun 18 '20 03:06 parker-mike

@jfgouda Do you think you can help me figure it out?

parker-mike avatar Jun 20 '20 00:06 parker-mike