Angular Error StaticInjectorError

I am writing a library that should wrap angular-oauth2-oidc .

Everything (library and consumer application) compiles fine, but at runtime in the browser I get the following error:

ERROR Error: StaticInjectorError[InjectionToken DocumentToken]: StaticInjectorError[InjectionToken DocumentToken]: NullInjectorError: No provider for InjectionToken DocumentToken! at _NullInjector.get (core.js:993) at resolveToken (core.js:1281) at tryResolveToken (core.js:1223) at StaticInjector.get (core.js:1094) at resolveToken (core.js:1281) at tryResolveToken (core.js:1223) at StaticInjector.get (core.js:1094) at resolveNgModuleDep (core.js:10878) at _createClass (core.js:10919) at _createProviderInstance$1 (core.js:10889) 

Short code:

Library module: Defines NgModule for my library, imports the required OAuthModule and HttpClientModule . Defines my SomeService .

 @NgModule({ imports: [ HttpClientModule, OAuthModule.forRoot() ], providers: [ SomeService ] }) export class SomeModule {} 

Library Service: A minimal example, does nothing but instantiate an OAuthService.

 @Injectable() export class SomeService { constructor(private oauth: OAuthService) {} } 

Consumer Application Module: Just imports my library as SomeModule .

 @NgModule({ ... imports: [ SomeModule ], bootstrap: [ AppComponent ] }) export class AppModule { } 

Consumer component: Trying to call the SomeService service in my library, which SomeService error.

 @Component({ ... }) export class AppComponent { constructor( private someService: SomeService ) {} } 
  • Library and application compiled without errors
  • When I go to the application in the browser, I get the error mentioned above.
  • If I HttpClientModule from the import library, it says that I skip the HttpClient instead of the error above
  • Library built with angular-library-seed
  • Application built using @angular/cli

I have tried many things, for example ...

  • If you have an application, also import the OAuthModule
  • Removing .forRoot() in the OAuthModule.forRoot() in the library means that the library cannot get into services inside OAuthModule
  • Changing a library that also uses .forRoot() , with .forRoot() about every combination of services in the providers array (between each other, OAuthService and HttpClientService).
  • Webpack configuration change for @ angular / * list as external
  • Webpack configuration change to display OAuth library as external
  • Using rollup instead of webpack with configurations from two separate source projects
  • More things that came down to just grabbing a straw

It seems that this error can happen if there is something obvious in the NgModule package in the provider array; it doesn't look like me. I do not use the "DocumentToken" anywhere.

I also saw that the general fix is ​​to import the HttpClientModule , which I already did to no avail.

I found exactly one case when someone else got this error using the "DocumentToken". They fixed their library in this commit , which seemed to just add more external to their folding configuration. Although my library uses webpack, it uses webpack-angular-externals to make sure all @ angular / * packages are listed as external.

Any help or ideas would be extremely, extremely appreciated.

(Angular 5, TypeScript 2.6.2)

+5
source share

Source: https://habr.com/ru/post/1274536/


All Articles