如何将信息从ngrx提取到组件文件

问题描述

我已将ngrx库添加到我的项目中,以便可以为我的数据创建更具响应性的模型。我已经设置了动作,减少效果文件。我相信这些文件可以按预期工作,因为将结果记录到效果文件中时,我能够看到填充的模型。但是,我无法将这些结果显示在组件中。我曾尝试在网上寻找解决方案,但没有运气。如果有人可以在我的代码中发现错误,请告诉我如何解决此问题。我将在效果和组件文件添加必要的代码,因为我相信这里存在问题。如果有人要求其他文件内容,我也会添加它。

效果

@Effect()
getoperatorFacts: Observable<any> = this.actions
.pipe(
  ofType(GetoperatorFacts),map((action) => action),switchMap(action => this.operatorSanityService.getoperatorsThatHaveOperatorFacts(action.payload)
    .pipe(
      map(  (results: OperatorFacts) => {
        GetoperatorFactsSuccess({payload: results});
        console.log('From effects file',results);
      }),catchError((err) => of( GetoperatorFactsFailure({payload: err})))
    )
  )
);

尝试从组件中获取内容

this.store.dispatch(GetoperatorFacts({payload: true}));
const x = this.store.pipe(select(getoperatorFactsstate));
x.subscribe(res => {
  console.log('ajde ajde',res);
});

以下代码吐出以下两个错误

core.js:4352 ERROR Error: Effect "OperatorFactsEffects.getoperatorFacts" dispatched an invalid action: undefined

ERROR TypeError: Actions must be objects

解决方案:

按Marek W的建议,在成功和失败功能之前添加return语句可以解决代码中的错误。我遇到的另一个问题是在选择器中我正在返回state.OperatorFacts。进行更改以使其仅返回状态是使信息出现在组件文件中的正确调用

解决方法

您需要返回新操作。

$result=$request->file('file')->store('MyFiles/'.id_message);

您的// code map((results: OperatorFacts) => { console.log('From effects file',results); return new GetOperatorFactsSuccess({payload: results}); }),// code 块同样适用。