每次打开组件时,状态都会重置

时间:2018-03-06 10:51:18

标签: angular ngrx-store-4.0

我有以下减速器:

export function partnerReducer(state = PartnerState.getInitialState(),  action: Actions): PartnerState {
 console.log('state', state)
 switch (action.type) {
  case STORE_PARTNER:
    return { ...state, partner: action.payload }
  case RESET_PARTNER:
    console.log('calling this')
    return { ...state, partner: action.payload }
  default:
    return state
 }
}

这是PartnerState

export default class PartnerState {
partner: Partner

static getInitialState() {
  const partnerState = new PartnerState()
  partnerState.partner = new Partner()

  return partnerState
 }
}

这是组件中的代码

 this.store.select(getPartner).subscribe((partner: Partner) => {
  let retrievedPartner = partner
  console.log('retrievedPartner', retrievedPartner)
  if (Util.isNull(retrievedPartner) || Util.isEmpty(retrievedPartner)) {
    console.log('retrieved partner is null')
    this.getPartner()
  } else {
    console.log('got a partner')
    this.partner = retrievedPartner
    this.getPartnerUsers()
  }
 })
}

this.getPartner()调用服务并检索合作伙伴。成功时,合作伙伴会像this.store.dispatch(new StorePartnerAction(response))

一样存储在商店中

但每当我在浏览器中刷新路径时,我都会收到此回复:

state 
    Object { partner: {} }
retrievedPartner 
    Object {  }
retrieved partner is null
state 
   Object { partner: {} }
retrievedPartner 
   Object { ... partner object}
got a partner

页面重新加载的预期响应:

 state 
    Object { partner: ...Partner Params }
retrievedPartner 
    Object { ... partner object}
got a partner

0 个答案:

没有答案