我最近問了一個關于處理異步資料的問題。我設法從服務器獲取資料。
但是無法更新頁面上的資料...
呼叫 getBook() 函式(app.component.ts)時,輸出:
Store {actionsObserver: ActionsSubject, reducerManager: ReducerManager, source: Store, operator: ?} '___'
app.component.html:
<app-layout></app-layout>
<button (click)="getBook()">get data</button>
效果.ts:
export class AppEffects {
constructor(private actions$: Actions, private service: StoreService) {}
update$ = createEffect(() => this.actions$.pipe(
ofType(GET_DATA),
exhaustMap(() =>
this.service.DataLoad()
.pipe(
map(
data => DATA_LOAD(data)
),
)
)
), {dispatch: false});
}
app.component.ts:
book$: Observable<any> = this.store.select((state:any) => state);
constructor(private store: Store) {}
ngOnInit() {
this.store.dispatch(GET_DATA());
}
getBook() {
console.log(this.book$, '___')
}
減速器.ts:
export const initialState: any = {
data: {}
};
export const BookReducer = createReducer(
initialState,
on(DATA_LOAD, (state, book) => book.results),
on(GET_DATA, state => state)
);
動作.ts:
export const DATA_KEY = 'DATA';
export const DATA_LOAD = createAction('[DATA] DATA_LOAD', (book:any) => book);
export const GET_DATA = createAction('[DATA] GET_DATA');
export const featureSelector = createFeatureSelector<any>(DATA_KEY);
uj5u.com熱心網友回復:
洗掉{dispatch: false}效果的配置。這使得加載操作不會被分派,因此減速器永遠不會收到更新狀態的操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/470962.html
上一篇:ngstore呼叫時不保存狀態
下一篇:路由角度時的堆疊訂閱
