ngrx/store - 'throw error as Cannot read property'
ngrx/store - 'throw error as Cannot read property'
我正在实施 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;
}
}
这里有错别字:
const getProductFeatureState = createFeatureSelector<ProductState>("product");
"products" 是您定义的内容,您选择的是 "product"
我正在实施 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;
}
}
这里有错别字:
const getProductFeatureState = createFeatureSelector<ProductState>("product");
"products" 是您定义的内容,您选择的是 "product"