Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared InjectionToken in a shared singleton service issue #35

Open
parker-mike opened this issue Jun 18, 2020 · 1 comment
Open

Shared InjectionToken in a shared singleton service issue #35

parker-mike opened this issue Jun 18, 2020 · 1 comment

Comments

@parker-mike
Copy link

parker-mike commented Jun 18, 2020

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant