4
votes

I have created the shared Angular 8 library with service which is responsible for making http calls to fetch some data. library fine, when I import into my module I am getting below error

I have added so the service will be added to root.

@Injectable({
  providedIn: 'root'
})

I have imported the service in AppModule Also, imported HttpClientModule

MyServiceProvider in lib

@Injectable({
  providedIn: 'root'
})

export class MyServiceProvider {

  private endpointUrl: string;

  constructor(
    private http: HttpClient,
    private proxy: ProxyService) {

    this.endpointUrl = this.proxy.getEndpoint();
  }

  getMydata() {

    return this.http.post(this.endpointUrl).pipe(
      map(response => {
        console.log(response);
      })
      , catchError((error: HttpErrorResponse) => {
        return throwError(error.error);
      }));
  }

}

in AppModule


import { MyServiceProvider } from 'my-lib';


@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    HttpClientModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    IonicStorageModule.forRoot()
  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    MyServiceProvider ---> (also tried without this)
  ],
  bootstrap: [AppComponent]
 })
 export class AppModule { }

ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[MyServiceProvider -> HttpClient]: StaticInjectorError(Platform: core)[MyServiceProvider -> HttpClient]:

1
Did you find any solution? I'm facing the very same issue where the DI doesn't provide the HttpClient to the library service even tho I import HttpClientModule into my root AppModule. (Angular 10)Baz
Do you have a my-service-provider.module.ts file? If yes, I think you need to import the HttpClientModule there.Travis Peterson

1 Answers

-1
votes

You should import HttpModule into your module.

import { HttpModule } from '@angular/http';

@NgModule({
    imports: [
        HttpModule
    ]
    ...
})