NGRX:TypeError:操作必须具有type属性

Thi*_*ibs 5 ngrx ngrx-effects angular

ngrx我是新手,但面临一个例外,不确定为什么...

我正在尝试使用dispatchaction并在中处理它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;
Run Code Online (Sandbox Code Playgroud)

效果:

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) {}
}
Run Code Online (Sandbox Code Playgroud)

零件:

this.store.dispatch(new MyActions.TryTest({ name: 'test' }));
Run Code Online (Sandbox Code Playgroud)

我在用:

效果:4.0.5,存储:4.0.3

Thi*_*ibs 8

如果这有助于其他人开始......结果证明我没有返回 ngrx 在地图运算符中期望的操作。

  • 我也发生了同样的事情,我正在返回(myaction)而不是 myaction (2认同)

Eng*_*Liu 5

因为效果必须在最后返回一个动作。因此,您有两种选择:

  1. 返回一个{type:string}对象
  2. 返回一个新的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) {}
}
Run Code Online (Sandbox Code Playgroud)

  • 请不要只邮编。而是添加一个解释。 (3认同)