gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:46:06 24 4
gpt4 key购买 nike

我正在尝试重新创建一个非常类似于 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com