NGRX:类型错误:操作必须有一个类型属性
问题描述:
作为新ngrx
我面临的一个例外,不知道为什么......NGRX:类型错误:操作必须有一个类型属性
我想dispatch
的action
并在effect
处理它,但我一直收到错误:TypeError: Actions must have a type property
操作:
export const TEST_ACTION = 'test_action';
export class TryTest implements Action {
readonly type = TEST_ACTION;
constructor(public playload: any) {
}
}
export type MyActions = TryTest;
影响:
import * as MyActions from "./myactions.actions";
@Injectable()
export class MyEffects {
@Effect()
testEffect = this.actions$
.ofType(MyActions.TEST_ACTION)
.map((action: MyActions.TryTest) => {
return 'something'
});
constructor(private actions$: Actions) {}
}
组件:
this.store.dispatch(new MyActions.TryTest({ name: 'test' }));
我使用:
效果:4.0.5和存储:4.0.3
答
如果这可以帮助其他人开始...原来我不是返回ngrx在map运算符中所期望的Action。
答
因为效果必须在最后返回一个动作。 所以你有两个选择:
- 回报{类型:字符串}对象
- 返回一个新的Action()
进口*从 “./myactions.actions” MyActions;
@Injectable()
export class MyEffects {
@Effect()
testEffect = this.actions$
.ofType(MyActions.TEST_ACTION)
.map((action: MyActions.TryTest) => {
return {type:'your_action'}
});
constructor(private actions$: Actions) {}
}
+3
请不要只发布邮政编码。相反,添加一个解释。 – tmuecksch
我认为你需要添加paranthesis('()')当你想派遣一个新的动作,否则构造不叫:'this.store.dispatch(新MyActions.TryTest({名称: 'test'})());' – David
感谢大卫的时间,但我想我调用构造函数并传递对象{name:test} – Thibs
哦,对不起,我误解了你的代码!没关系! – David