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