ngrx/store-‘因无法读取属性而引发错误’
2019-01-25
105
我正在实现
createFeatureSelector
和
createSelector
- 但出现错误
core.js:15714 ERROR TypeError: Cannot read property 'showProductCode' of undefined
我使用
"@ngrx/store": "^7.1.0",
但找不到问题。这是我的代码:
import { Product } from "./../product";
import * as fromRoot from "./../../state/app.state";
import { createFeatureSelector, createSelector } from "@ngrx/store";
export interface State extends fromRoot.State {
products:ProductState
}
export interface ProductState {
showProductCode : boolean;
currentProduct : Product;
products:Product[]
}
const initialState:ProductState = {
showProductCode : true,
currentProduct:null,
products:[]
}
const getProductFeatureState = createFeatureSelector<ProductState>("product");
export const getShowProductCode = createSelector(
getProductFeatureState,
state => state.showProductCode
);
export const getCurrentProduct = createSelector(getProductFeatureState, state => state.currentProduct);
export const getProducts = createSelector(getProductFeatureState, state => state.products);
export function reducer(state=initialState, action):ProductState {
switch (action.type) {
case "TOGGLE_PRODUCT_CODE":
return {
...state,
showProductCode : action.payload
}
default:
return state;
}
}
1个回答
此处输入有误:
const getProductFeatureState = createFeatureSelector<ProductState>("product");
“products”是您定义的,而您选择的是“product”
Flignats
2019-01-25