Nbo*_*onD 2 ngrx ngrx-effects angular angular5
我正在尝试重新创建一个非常类似于ngrx文档的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'}))
))
);
}
Run Code Online (Sandbox Code Playgroud)
app.module:
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
StoreModule.forRoot(rootReducer),
StoreDevtoolsModule.instrument({
maxAge: 10
}),
EffectsModule.forRoot([HttpEffects])
],
exports: [
BrowserAnimationsModule
],
providers: [ HttpClient ],
bootstrap: [AppComponent]
})
Run Code Online (Sandbox Code Playgroud)
我也尝试在功能模型中导入效果服务EffectsModule.forFeature(),但是会抛出相同的错误.
在使用HttpClient之前,需要安装提供它的HttpClientModule.这可以在您的应用程序模块中完成,只需要一次.
所以你必须导入HttpClientModule而不是HttpClient:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
HttpClientModule,
...
EffectsModule.forRoot([HttpEffects])
],
exports: [
BrowserAnimationsModule
],
bootstrap: [AppComponent]
})
Run Code Online (Sandbox Code Playgroud)