没有nttx / effects的HttpHandler提供程序

时间:2018-01-28 14:36:25

标签: angular angular5 ngrx ngrx-effects

我正在尝试重新创建一个与ngrx docs非常相似的authEffect,并收到以下错误消息: Error: StaticInjectorError(AppModule)[HttpClient -> HttpHandler]: StaticInjectorError(Platform: core)[HttpClient -> HttpHandler]: NullInjectorError: No provider for HttpHandler!

效果服务:

@Injectable()
export class HttpEffects {
  constructor(private http: HttpClient, private actions$: Actions) {}

  @Effect() login$: Observable<ActionWithPayload> = this.actions$.pipe(
    ofType(ActionTypes.SEND_LOGIN),
    mergeMap((action: ActionWithPayload) =>
      this.http.post(SERVER_URL + LOGINAPI.LOGIN, action.payload, config).pipe(
        map(data => ({type: 'LOGIN_SUCCESS', payload: data})),
        catchError(() => of({type: 'LOGIN_ERROR'}))
      ))
  );
}

app.module:

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    StoreModule.forRoot(rootReducer),
    StoreDevtoolsModule.instrument({
      maxAge: 10
    }),
    EffectsModule.forRoot([HttpEffects])
  ],
  exports: [
    BrowserAnimationsModule
  ],
  providers: [ HttpClient ],
  bootstrap: [AppComponent]
})

我也尝试使用EffectsModule.forFeature()在功能模型中导入效果服务,但会抛出相同的错误。

1 个答案:

答案 0 :(得分:5)

HttpClient documentation中所述:

  

在使用HttpClient之前,您需要安装   提供它的HttpClientModule。这可以在你的   应用程序模块,只需要一次。

所以你必须导入 HttpClientModule 而不是 HttpClient

import { HttpClientModule } from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    ...
    EffectsModule.forRoot([HttpEffects])
  ],
  exports: [
    BrowserAnimationsModule
  ],
  bootstrap: [AppComponent]
})