NgXs dynamic selector: TypeError: Cannot read property 'x' of undefined

NgXs dynamic selector: TypeError: Cannot read property 'x' of undefined

我正在尝试创建 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];
        });
    }
    ...
}

原因是 @Selector() 装饰器,它旨在仅与 lazy selectors 一起使用,而不是动态。我去掉了装饰器,问题就解决了。

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