我寫了一個簡單的 ngrx 專案來測驗存盤,但是我得到了無限回圈但是我嘗試復制其他示例而沒有回圈。首先,我定義了 2 個類模型如下:
export interface IBookRecords {
id? : number;
bookNumber?: string;
guestID ? : number;
guestName ? :string;
bookdetails? : IBookDetail[];
}
export interface IBookDetail {
id? : number;
roomName?: string;
noOfGuest?:number;
}
對于 action.ts ,我寫這個
requestSingleRecord : createAction('[Booking] Load booking record'),
requestSingleRecordSuccess : createAction('[Booking] Load booking record', props<{booker: IBookRecords}>() )
對于 effect.ts ,我寫這個
loadBookRecords1$ = createEffect(() => this.action$.pipe
(
ofType(appActions.requestSingleRecord),
mergeMap(() =>
this.rmservice.SingleBookRecord()
.pipe(
switchMap(booker => [
appActions.requestSingleRecordSuccess({ booker })])
)
)
)
);
對于 reducer.ts ,我寫這個
export interface IAppState {
booker : IBookRecords
}
export const initialState: IAppState = {
booker:{}
}
export const appReducer = createReducer(
initialState,
on(appActions.requestSingleRecordSuccess, (state, action) => ({
...state, booker :action.booker
})),
on(appActions.updaterecord, (state, action) => ({
...state, booker :action.booker
}))
);
對于 selector.ts ,我寫這個
const selectBookRecordFeature = createFeatureSelector<IAppState>('booker');
export const selectBookRecordCs = createSelector (
selectBookRecordFeature,
(state : IAppState) => state.booker
);
然后,當我this.store.dispatch(appActions.requestSingleRecord());
在 ngOnInit() 函式下寫入appcomponent時 ,它會無限回圈
任何人都可以建議嗎?
謝謝
uj5u.com熱心網友回復:
由于 requestSingleRecord 和 requestSingleRecordSuccess 的操作型別相同。這是無限回圈,嘗試更改操作型別,例如
requestSingleRecord : createAction('[Booking] Load booking record'),
requestSingleRecordSuccess : createAction('[Booking] Load booking record sucess', props<{booker: IBookRecords}>() )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322911.html
上一篇:角度,將輸入欄位重置回默認值
