我有一種方法來調度查詢帳戶并選擇帳戶的操作。我不確定這是否是調度后選擇資料的最佳做法。
this._store.dispatch(AccountsPageActions.loadAccountWithDetails({ accountId: this._accountId }));
this.account$ = this._actionsSubject.pipe(
filter(action => action.type === AccountsApiActions.loadAccountWithDetailsSuccess.type),
switchMap(() => this._store.select(getAccountDetailById(this._accountId)).pipe(
tap(account => {
this.account = account;
this.accountId = this._accountId;
this._reportsService.setAccount(account);
})
))
);
有人可以告訴我一個更好的做法,或者這是要走的路嗎?
uj5u.com熱心網友回復:
我不知道你為什么需要動作主題,你可以使用下面的代碼訂閱動作,只要有該動作的調度,它就會觸發。在建構式中保留動作訂閱,然后您可以在組件中的任何地方分派動作
PLUCK用于從有效負載中獲取 accountId 值
import { Actions, ofType } from '@ngrx/effects';
import { map, pluck, switchMap, tap } from 'rxjs/operators';
...
constructor(private action: Actions) {
const sub = this.action.pipe(
ofType(AccountsApiActions.loadAccountWithDetailsSuccess.type),
pluck('accountId'),
switchMap((accountId) => this._store.select(getAccountDetailById(accountId)).pipe(
tap(account => {
this.account = account;
this.accountId = this._accountId;
this._reportsService.setAccount(account);
}))).subscribe();
}
ngOnInit() {
this._store.dispatch(AccountsPageActions.loadAccountWithDetails({ accountId: this._accountId }));
}
uj5u.com熱心網友回復:
你不需要聽動作調度。如果設計正確,您的操作將更新狀態并更新選擇器。這就夠了
ngOnInit() {
this._store.dispatch(AccountsPageActions.loadAccountWithDetails({ accountId: this._accountId }));
// If you want to use the account$ with async pipe
account$ = this._store.select(getAccountDetailById(this._accountId)).pipe(
filter(account => !!filter), // ignore if no account is returned
tap(account => {
this.account = account;
this.accountId = this._accountId;
this._reportsService.setAccount(account);
})
); // or .subscribe();
}
我會避免監聽組件中的動作調度,為此使用效果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/352596.html
標籤:javascript 有角的 打字稿 rxjs ngrx
