没有第一次加载的数据

佐纳

我是 NgRx 的新手,并尝试使用Effectshttp 请求检索和缓存分页表数据但是在任何第一次页面加载时(如果页面尚未缓存)我得到了空页面,即使我执行状态对象的 console.log,我看到里面的数据?当我进入上一页时,数据就在那里,所以我猜测异步世界中的某些事情我做错了,但无法弄清楚是什么:/

这是我的初始化 component.ts

  ngAfterViewInit() {

    this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);

    merge(this.sort.sortChange, this.paginator.page)
      .pipe(
        startWith({}),
        switchMap(() => {
          this.isLoadingResults = true;

          this.store.dispatch(new ListLoad(this.getQueryParams()));

          return this.store.pipe(select('list'));
         }),
        map((state: State) => {
          this.isLoadingResults = false;
          this.resultsLength = state.totalRecords;

          return this.cacheKey in state.data ? state.data[this.cacheKey] : [];
        }),
        catchError((err) => {
          this.isLoadingResults = false;
          this.resultsLength = 0;

          return observableOf([]);
        })
      )
      .subscribe((data: any[]) => {
        return this.data = data
      });
  }

这是我的效果定义 effects.ts

@Effect()
loadData = this.actions$.pipe(
    ofType(actions.actionTypes.ListLoad),
    mergeMap((action: actions.actionTypes.ListLoadSuccess) => this.service.getAll(action.payload).pipe(
        map(
            response => {
                let apiResponse = new ApiResponse(response);
                let cacheKey = JSON.stringify(action.payload);

                return apiResponse.isSuccess ?
                    new actions.ListLoadSuccess({ key: cacheKey, data: apiResponse.data }) :
                    new actions.ListLoadFailed(`code: ${apiResponse.status.error_code}; message: ${apiResponse.status.error_message}`);
            }
        ),
        catchError(err => observableOf(new actions.ListLoadFailed(err)))
    ))
)

除此之外,如果 NgRx 存储中存在包含数据的页面,我想取消 http 请求

佐纳

我能够解决它。问题是我正在通过向其添加新属性来更新作为对象的 store 的属性。Store 不会发出片段更新的事件,因此不会触发 Select 订阅。我引入了另一个用于加载状态的布尔参数,我监听更改,如果加载为假(页面已加载),我选择所需的片段。我还为页面缓存添加了额外的代码

component.ts

  ngOnInit() {

    this.isLoadingResults$ = this.store.pipe(
      select(state => state.stateFragment.isListLoading),
      takeWhile(() => this.componentActive) //unsubscribe
    );

    this.store.dispatch(new ListLoad());

    this.isLoadingResults$.pipe(
      filter((isLoading:boolean) => !isLoading),
      switchMap(() => this.store.pipe(
        select(state => state.stateFragment),
        takeWhile(() => this.componentActive) //unsubscribe
      )),
      map(...)
    ).subscribe(...);

    //Other stuff here

  }

effects.ts

@Effect()
    load$ = this.actions$.pipe(
        ofType(actions.actionTypes.ListLoad),
        withLatestFrom(this.store.pipe(select(state.stateFragment))),
        filter(([action, store]) => {
            let isPageCached: boolean = action.payload in store.stateFragment;
            if (isPageCached) {
                this.store.dispatch(new actions.ListLoaded()); //for sake of changing loading state
            }
            return !isPageCached;
        }),
        switchMap(([action, store]) => {
            return this.service.getAll(action.payload).pipe(
                map(
                    response => {
                        let apiResponse = new ApiResponse(response);
                        return apiResponse.isSuccess ?
                            new actions.ListLoadSuccess({ key: action.payload, data: apiResponse.getData(), totalRecords: apiResponse.getTotalCount() }) :
                            new actions.ListLoadFailed(`code: ${apiResponse.status.error_code}; message: ${apiResponse.status.error_message}`);
                    }
                ),
                catchError(err => observableOf(new actions.ListLoadFailed(err)))
            );
        }
        ), share()
    )

reducer.ts

export function reducer(state = initialState, action: Actions) {
    switch (action.type) {
        case actionTypes.ListLoad:
            return {
                ...state,
                isListLoading: true
            };
        case actionTypes.ListLoaded:
            return {
                ...state,
                isListLoading: false
            };
        case actionTypes.ListLoadSuccess:
            state.listData[action.payload.key] = action.payload.data;

            return {
                ...state,
                isListLoading: false,
                listData: state.listData,
                listTotal: action.payload.totalRecords
            };
        case actionTypes.ListLoadFailed:
            return {
                ...state,
                isListLoading: false,
                error: action.payload
            };
        case actionTypes.ListClear:
            return {
                ...state,
                listData: {},
                listTotal: 0
            };;
        default:
            return state;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

页面加载后,第一次没有立即调用Ajax

来自分类Dev

GridView第一次没有被点击

来自分类Dev

Favicon第一次没有出现

来自分类Dev

钩子第一次没有设置状态

来自分类Dev

javascript没有看到第一次点击?

来自分类Dev

GridView第一次没有被点击

来自分类Dev

第一次尝试 RSepc 没有运气

来自分类Dev

第一次没有创建数据库

来自分类Dev

第一次加载页面

来自分类Dev

第一次加载页面时,如何使网格不加载数据?

来自分类Dev

为什么Neo4j查询没有在第一次查询中返回wright数据?

来自分类Dev

FirebaseRecyclerAdapter-populateViewHolder第一次运行时没有填充数据吗?

来自分类Dev

为什么我没有在第一次调用我的 REST API 时获取数据

来自分类Dev

流星:从Iron Router传递到模板的数据,在第一次加载时为空

来自分类Dev

第一次加载数据时使用SwipeRefreshLayout或ProgressBar

来自分类Dev

Angular2 - sa-datatable 仅第一次加载数据

来自分类Dev

PrimeFaces 数据表分页器在第一次延迟加载时不显示

来自分类Dev

当点击元素时,ajax请求第一次加载数据,第一次后阻止ajax请求

来自分类Dev

Bootstrap崩溃第一次没有动画,并且在第一次之后有抖动的动画

来自分类Dev

Bootstrap崩溃第一次没有动画,并且在第一次之后有抖动的动画

来自分类Dev

第一次错误使用Picasso加载图片

来自分类Dev

蚀第一次加载错误

来自分类Dev

serviceworker第一次不加载

来自分类Dev

未加载JavaScript代码(仅第一次)

来自分类Dev

网页未在第一次加载

来自分类Dev

UICollectionView第一次加载时明显滞后

来自分类Dev

UITableViewCell第一次未正确加载标签

来自分类Dev

避免第一次加载图像

来自分类Dev

仅在第一次或刷新时加载Javascript

Related 相关文章

  1. 1

    页面加载后,第一次没有立即调用Ajax

  2. 2

    GridView第一次没有被点击

  3. 3

    Favicon第一次没有出现

  4. 4

    钩子第一次没有设置状态

  5. 5

    javascript没有看到第一次点击?

  6. 6

    GridView第一次没有被点击

  7. 7

    第一次尝试 RSepc 没有运气

  8. 8

    第一次没有创建数据库

  9. 9

    第一次加载页面

  10. 10

    第一次加载页面时,如何使网格不加载数据?

  11. 11

    为什么Neo4j查询没有在第一次查询中返回wright数据?

  12. 12

    FirebaseRecyclerAdapter-populateViewHolder第一次运行时没有填充数据吗?

  13. 13

    为什么我没有在第一次调用我的 REST API 时获取数据

  14. 14

    流星:从Iron Router传递到模板的数据,在第一次加载时为空

  15. 15

    第一次加载数据时使用SwipeRefreshLayout或ProgressBar

  16. 16

    Angular2 - sa-datatable 仅第一次加载数据

  17. 17

    PrimeFaces 数据表分页器在第一次延迟加载时不显示

  18. 18

    当点击元素时,ajax请求第一次加载数据,第一次后阻止ajax请求

  19. 19

    Bootstrap崩溃第一次没有动画,并且在第一次之后有抖动的动画

  20. 20

    Bootstrap崩溃第一次没有动画,并且在第一次之后有抖动的动画

  21. 21

    第一次错误使用Picasso加载图片

  22. 22

    蚀第一次加载错误

  23. 23

    serviceworker第一次不加载

  24. 24

    未加载JavaScript代码(仅第一次)

  25. 25

    网页未在第一次加载

  26. 26

    UICollectionView第一次加载时明显滞后

  27. 27

    UITableViewCell第一次未正确加载标签

  28. 28

    避免第一次加载图像

  29. 29

    仅在第一次或刷新时加载Javascript

热门标签

归档