Agular Store 效果不等待选择器数据
我正在使用 NgRx store 开发一个 Angular 项目。所有后端响应都保存在商店中。
这是我的 effects.ts
loadData$ = createEffect(() => this.actions$.pipe(
ofType('[Data] Load Data'),
concatMap((action: any) => of(action).pipe(
withLatestFrom(this.store$.pipe(select(selectorData)))
)),
switchMap(([, request]: any) => this.Service.getDataApi(
request.data1.iid,
request.data2.iid2
)
.pipe(
map((data: Data[]) => ({
type: '[Data] Load Success',
payload: { data },
}
)),
catchError((error: any) => of({
type: '[Datae]] Load Failure',
payload: { error },
}
))
))
)
);
我如何等待 selectorData 数据? 有没有具体的方法呢? 当前的实现 idd 和 iid2 为空。
提前致谢。
编辑1 我做了以下更改。
concatMap((action: any) => of(action).pipe(
withLatestFrom(this.store$.pipe(select(selectorData),skipWhile(iid => !iid),
skipWhile(iid2 => !iid2),
mapTo(true))
)
以上更改不会使用 API 进行调用。如果数据可用,我该如何调用
编辑 2
filter(([, {configData}]: any) => configData.iid &&
configData.iid)
我也尝试过过滤。第一次configData.iid未定义。我们如何解决这个问题
它抛出未定义的错误
编辑3 我已尝试 combineLatestWith。它不等待 this.store$.pipe(select(selectorData))
等待数据还有其他解决方案吗?
Am working on an Angular project using NgRx store. All the back end responses are saved in the store.
This is my effects.ts
loadData$ = createEffect(() => this.actions$.pipe(
ofType('[Data] Load Data'),
concatMap((action: any) => of(action).pipe(
withLatestFrom(this.store$.pipe(select(selectorData)))
)),
switchMap(([, request]: any) => this.Service.getDataApi(
request.data1.iid,
request.data2.iid2
)
.pipe(
map((data: Data[]) => ({
type: '[Data] Load Success',
payload: { data },
}
)),
catchError((error: any) => of({
type: '[Datae]] Load Failure',
payload: { error },
}
))
))
)
);
How can i wait for selectorData data?
Is there any specific method for that?
Current implementation idd and iid2 are empty.
Thanks in advance.
Edit 1
I have made below changes
concatMap((action: any) => of(action).pipe(
withLatestFrom(this.store$.pipe(select(selectorData),skipWhile(iid => !iid),
skipWhile(iid2 => !iid2),
mapTo(true))
)
Above changes it will not call with the API.how can i call if the data is available
Edit 2
filter(([, {configData}]: any) => configData.iid &&
configData.iid)
I have tried filter as well. First time configData.iid is undefined.how can we solve this issue
It throwing undefined error
Edit 3
I have tried combineLatestWith.it doesn't wait for this.store$.pipe(select(selectorData))
Any other solution for waiting data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
combineLatest
来等待多个操作或源可观察量。You can use
combineLatest
to wait for multiple actions or source observables.