javascript - NGRX 操作已调度,但效果未触发
问题描述
我遇到了一个问题,我的 NGRX 效果没有触发。这是我的动作、reducers、效果和组件代码。
这是我的效果中的代码片段,我只用占位符替换了实体和服务的实际名称。
// entity.effects.ts
createEntity$: Observable<Action> = this.actions$.pipe(
ofType<CreateEntity>(EntityActionTypes.CreateEntity),
map(action => {
console.log("here");
return action.payload;
}),
mergeMap(data => {
return this.service.createEntity(data).pipe(
map(data => new CreateEntitySuccess(data)),
catchError(error => of(new CreateEntityError(error)))
);
})
);
entity.actions.ts
import { Action } from "@ngrx/store";
export enum EntityActionTypes {
CreateEntity = "[Entity API] Create Entity",
CreateEntitySuccess = "[Entity] Create Entity Success",
CreateEntityError = "[Entity] Create Entity Error"
}
export class CreateEntity implements Action {
readonly type = CreateEntityActionTypes.CreateEntity;
constructor(public payload: any) {}
}
// ... CreateEntitySuccess and CreateEntityError are defined here...
export type EntityActionsUnion =
| CreateEntity
| CreateEntitySuccess
| CreateEntityError;
本质上,我有一个容器,它在表单提交时调度 CreateEntity 操作。但是,我没有看到我写入效果的 console.log()。此外,我对加载从正在工作的 LoadEntities 上的 REST API 发出对所有实体的请求有另一个效果。然而,创建一个新实体的效果不是,我什至不认为它正在触发。
我也没有收到任何错误。这是调度我的操作的代码:
import * as EntityActions from '../actions/entity.actions.ts
createBuilding() {
const data = this.form.value;
const image = this.fileUploadComponent.file;
const payload = {
data,
image
};
this.store.dispatch(new EntityActions.CreateEntity(payload));
}
我有 Redux 开发工具,我看到调度触发和loading: true
返回的新状态。但是,效果没有触发,我的 Success 或 Error 调度也没有触发分辨率。知道为什么会这样吗?
解决方案
你装饰过你的效果方法@Effect()
吗?这就是我通常想念的:)
推荐阅读
- c - esp32 在等待 XQueueRecieve 监视 uart 中断时崩溃
- arrays - 在 Rust 中将图像转换为 Rulinalg 矩阵
- javascript - MapBox GL JS:从通过代码设置的字符串地址获取经纬度坐标
- c# - SaveChanges 的 EF Core 全局查询过滤器(更新、删除)
- java - JIT 编译器是否利用长期记忆来跟踪优化的方法?
- c# - .net Web API 中的匿名类型语法
- react-native - 在 React Native 中完全删除 Realm 数据库
- javascript - 如何在使用节点 js 的谷歌 vm 实例、谷歌托管证书和谷歌域上设置 ssl
- discord.py - 如何修复导入目录?
- reactjs - 超过最大更新深度。当组件在 useEffect 中调用 setState 时会发生这种情况,但 useEffect 要么没有依赖数组