NgXs动态选择器:TypeError:无法读取未定义的属性“ x”

时间:2019-11-15 09:00:42

标签: angular ngxs

我正在尝试创建NgXs dynamic selector,但是使用选择器时出现运行时错误:

TypeError: Cannot read property 'x' of undefined
    at my-state.state.ts:56
    at wrappedSelectorFn (ngxs-store.js:2136)
    at memoized (ngxs-store-internals.js:58)
    at memoized (ngxs-store-internals.js:58)
    at Store.selectSnapshot (ngxs-store.js:2343)
    ...

选择器代码:

@State({ ... })
export class MyState {
    @Selector()
    static translations(lng: model.Language) {
        return createSelector([MyState], (state: MyStateModel) => {
            return state.translations[lng];
        });
    }
    ...
}

1 个答案:

答案 0 :(得分:1)

原因是@Selector()装饰器,该装饰器只能与lazy selectors一起使用,不能与动态特性一起使用。我删除了装饰器,问题解决了。

@State({ ... })
export class MyState {
    // no decorators here
    static translations(lng: model.Language) {
        return createSelector([MyState], (state: MyStateModel) => {
            return state.translations[lng];
        });
    }
    ...
}