ngrx / store-“由于无法读取属性而引发错误”

时间:2019-01-25 10:44:48

标签: ngrx ngrx-store

我正在实现createFeatureSelectorcreateSelector-但收到错误消息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 个答案:

答案 0 :(得分:1)

您在这里有错字:

const getProductFeatureState = createFeatureSelector<ProductState>("product");

“产品”就是您所定义的,并且您正在选择“产品”