Angular ngrx商店没有获得最新价值

时间:2017-10-26 13:42:23

标签: angular ngrx ngrx-store ngrx-effects

我将参数传递给“帐户详细信息”页面,该页面调用Web Api以获取帐户详细信息。

我第一次加载“帐户明细”页面时,没有显示任何内容,如果我进入另一个帐户,则会显示上一个帐户的详细信息(这将继续,即下一个帐户将显示上一个帐户)。 / p>

我已经尝试更改调度和选择的顺序,也尝试将调度移动到构造函数但我仍然得到相同的行为。

这就是我目前正在做的事情......

帐户详细信息 - (CustomerAccountDetailed包含值)

actionsSubscription: Subscription;
customerAccount$: Observable<CustomerAccountDetailed>;

constructor(private store: Store<IAppState>,
    private actions: SalesLedgerActions,
    private route: ActivatedRoute) {
}

ngOnInit(): void {
    this.actionsSubscription = this.route.params.subscribe(params => {
        this.store.dispatch(this.actions.fetchSingleCustomerAccount(params.id));
        this.customerAccount$ = this.store.select(getCustomerAccount);
    });
  }

public ngOnDestroy(): void  {
    this.actionsSubscription.unsubscribe();
  }

SalesLedgerEffects

@Effect()
FetchSingleAccount$ = this.actions$
   .ofType(SalesLedgerActions.FETCH_CUSTOMER_ACCOUNT)
   .switchMap((action: Action) => {
      return this.customerApiService.getCustomerDetail(action.payload);
})
   .map((customerAccount: ItemDto<CustomerAccountDetailed>) => this.actions.fetchSingleCustomerAccountSuccess(customerAccount));

salesLedgerReducer

case SalesLedgerActions.FETCH_CUSTOMER_ACCOUNT_SUCCESS:
    const customerAccount: ItemDto<CustomerAccountDetailed> = payload as ItemDto<CustomerAccountDetailed>;
    return Object.assign({}, state, { selectedAccount: customerAccount.result as CustomerAccountDetailed }) as ISaleLedgerState;

选择

export function fetchCustomerRecord(state: ISaleLedgerState): any {
return state.selectedAccount;
}

// *************************** PUBLIC API's ****************************
export const getCustomerAccount: any = createSelector(getSalesLedgerState, fetchCustomerRecord);

1 个答案:

答案 0 :(得分:2)

将其移出subscription并将其放入构造函数中:

this.customerAccount$ = this.store.select(getCustomerAccount);
      });