angular – NGRX:TypeError:Actions必须具有type属性

前端之家收集整理的这篇文章主要介绍了angular – NGRX:TypeError:Actions必须具有type属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
作为ngrx的新手,我面临一个例外,不知道为什么……

我试图调度一个动作并在一个效果中处理它,但我不断收到错误:TypeError:Actions必须有一个type属性

操作:

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

解决方法

因为效果必须在最后返回一个动作.
所以你有两个选择:

>返回一个{type:string}对象
>返回一个新动作()

从“./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) {}
}

猜你在找的Angularjs相关文章