来自NGRX的异步数据

发布于 2025-01-27 04:50:02 字数 1646 浏览 1 评论 0原文

我最近问了一个有关使用异步数据的问题。 我设法从服务器获取数据。

但是不可能更新页面上的数据...

调用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>

fexive.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$, '___')

  }

reducer。 TS:

export const initialState: any = {
  data: {}
};

export const BookReducer = createReducer(
  initialState,
  on(DATA_LOAD, (state, book) => book.results),
  on(GET_DATA, state => state)
);

Action.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);

I recently asked a question about working with asynchronous data.
I managed to get data from the server.

but it is not possible to update the data on the page...

when calling the getBook() function (app.component.ts), output:

Store {actionsObserver: ActionsSubject, reducerManager: ReducerManager, source: Store, operator: ƒ} '___'

app.component.html:

  <app-layout></app-layout>
<button (click)="getBook()">get data</button>

effect.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$, '___')

  }

reducer.ts:

export const initialState: any = {
  data: {}
};

export const BookReducer = createReducer(
  initialState,
  on(DATA_LOAD, (state, book) => book.results),
  on(GET_DATA, state => state)
);

action.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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2025-02-03 04:50:02

删除效果的{dispatch:false}配置。
这使得没有派遣负载操作,因此还原器永远不会收到更新状态的操作。

Remove the {dispatch: false} config of the effect.
This makes it so the load action isn't dispatched, and thus the reducer never receives the action to update the state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文