ngrx 选择器订阅多次引发事件......在商店中有实际价值,然后是未定义的,然后是空值
ngrx selector subscription raising event multiple time ...with real value in store then undefined and then empty value
我有一个项目,我在其中实施了 ngrx 商店。
我的商店包含购物车作为商品列表。
我为此添加了选择器
const getUserState=(state:AppState)=>state.user || {};
export const getCarts=createSelector(getUserState,fromUser.getCart)
但是当我订阅选择器时,它被调用了多次(在我的例子中,第一次调用了 3 次正确的值,第二次未定义,第三次调用空值)
我的订阅如下所示
this.cartSubscription=this.store.select(getCarts).subscribe((cart)=>{
console.log('---------in dashboard--------')
console.log(cart);
this.addedItems=cart;
});
知道为什么会这样吗。
其中一项操作是使状态为空。
此行为的原因:
因为我已经有效地处理了那个动作,但是我错过了 reducer 中动作类型的默认切换案例。
这就是为什么它在发送该操作后返回我的空状态。
export function reducer(state:State=initialState,action:userActions){
switch(action.type){
case uActions.GET_ALL_FOOD_ITEMS_FINISHED:
return Object.assign({}, state, { foodItems: action.payload});
default: return state;
}
}
默认大小写救了我:)
我有一个项目,我在其中实施了 ngrx 商店。
我的商店包含购物车作为商品列表。
我为此添加了选择器
const getUserState=(state:AppState)=>state.user || {};
export const getCarts=createSelector(getUserState,fromUser.getCart)
但是当我订阅选择器时,它被调用了多次(在我的例子中,第一次调用了 3 次正确的值,第二次未定义,第三次调用空值)
我的订阅如下所示
this.cartSubscription=this.store.select(getCarts).subscribe((cart)=>{
console.log('---------in dashboard--------')
console.log(cart);
this.addedItems=cart;
});
知道为什么会这样吗。
其中一项操作是使状态为空。
此行为的原因:
因为我已经有效地处理了那个动作,但是我错过了 reducer 中动作类型的默认切换案例。
这就是为什么它在发送该操作后返回我的空状态。
export function reducer(state:State=initialState,action:userActions){
switch(action.type){
case uActions.GET_ALL_FOOD_ITEMS_FINISHED:
return Object.assign({}, state, { foodItems: action.payload});
default: return state;
}
}
默认大小写救了我:)