Skip to content

Shared InjectionToken in a shared singleton service issue #35

@parker-mike

Description

@parker-mike

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions