2

如何将 DataSource 加载方法与 ngrx 存储一起使用?

我有这些问题: 1.当页面加载时,加载方法被调用 2.无限加载 3. 2个请求被发送到服务器而不是1个

如果我直接使用该服务,那么不会有任何问题。

打字稿:

this.ds = new CustomStore({
  load: (loadOptions: any) => {
    this.myFacade.loadAllRecords(this.filter, loadOptions);
    return this.myFacade.records$
      .toPromise()
      .then(result => {
        return result;
      });
  }
});

无限加载

this.ds = new CustomStore({
  load: (loadOptions: any) => {
    this.myFacade.loadAllRecords(this.filter, loadOptions);
    return new Promise(resolve => this.myFacade.records$
      .pipe(takeUntil(this.unsubscribe$)).subscribe(resolve)).then(result => {
        return result;
      });
  }
});

首次加载
运行命令 - this.dataGrid.instance.refresh() - 2 个请求被发送到服务器而不是 1 个

export class MyFacade {
  public records$: Observable<any>;
  constructor(private store: Store<State>) {
    this.records$ =
      this.store.pipe(select(myQuery.getRecords));
  }
  loadAllRecords(model: myModel, loadOptions?: LoadOptions) {
    this.store.dispatch(new LoadRecords(model, loadOptions));
  }
}
4

1 回答 1

1

我认为问题是你的 Observablerecords$没有完成。并且toPromise()仍在等待 Observable 的解析。

我会做以下事情:

在门面添加take(1)

this.records$ =
  this.store.pipe(
    select(myQuery.getRecords),
    take(1)
  );

然后更改CustomStore

this.ds = new CustomStore({
  load: (loadOptions: any) => {
    this.myFacade.loadAllRecords(this.filter, loadOptions);
    return this.myFacade.records$
      .pipe(
        takeUntil(this.unsubscribe$)
      ).toPromise();
  }
});
于 2021-02-08T12:59:46.047 回答