服务缺少HttpClient的角度库

时间:2019-10-25 17:33:45

标签: angular angular-ui-router angular7

我用服务创建了共享的Angular 8库,该库负责进行http调用以获取一些数据。库很好,当我导入我的模块时,我遇到了错误

我已添加,因此该服务将添加到root。

@Injectable({
  providedIn: 'root'
})

我已经在AppModule中导入了服务 另外,导入的HttpClientModule

lib中的MyServiceProvider

@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);
      }));
  }

}

在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 { }

  

错误错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[MyServiceProvider-> HttpClient]:     StaticInjectorError(平台:核心)[MyServiceProvider-> HttpClient]:

1 个答案:

答案 0 :(得分:-1)

您应该将HttpModule导入模块。

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

@NgModule({
    imports: [
        HttpModule
    ]
    ...
})
相关问题