如何使用数据源负载方法与NGRX店?
我有以下问题: 1.加载页面时,将调用load方法 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;
});
}
});
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));
}
}
答案 0 :(得分:1)
我认为问题是你的 Observable records$
没有完成。而 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();
}
});