angular - 没有带有ngrx/效果的HttpHandler的提供者

标签 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'}))
      ))
  );
}

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

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

最佳答案

HttpClient documentation 中所述:

Before you can use the HttpClient, you need to install the HttpClientModule which provides it. This can be done in your application module, and is only necessary once.



所以你必须导入 HttpClientModule 而不是 HttpClient:
import { HttpClientModule } from '@angular/common/http';

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

关于angular - 没有带有ngrx/效果的HttpHandler的提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48487473/

相关文章:

javascript - 以 Angular 将文件转换为 uInt8Array

javascript - Moment.js 在 Mozilla Firefox 中显示无效日期

angular - 在 typescript Angular 5 中导入 json 文件

javascript - Twitter OAuth API 中的 CORS 问题

angular5 - 在 PrimeNG 中过滤数据表

javascript - 如何在控制台日志之外获取IPC消息的值?

javascript - 子路由在延迟加载中不起作用

javascript - ngrx 订阅者内部的 API 调用

angular - 如何使 ngrx-store 与 canLoad 防护一起使用

Angular Material 分页器无法处理异步数据