如何立即从状态中恢复数据?
How to recover data from a state instantly?
我开始使用 NgRx,但对访问 State 中包含的数据有点迷茫。
在问我的问题之前,我搜索了几个小时,因为我没有真正找到答案。
当使用 NgRx 将数据显示在列表中时,它很棒。
我想更多地使用 NgRx 将我的应用程序设置与我的 SettingsState 分组。
我想在我的 SettingsState 中特别说明:
- API 请求的基础 URL,
- 列表中显示的行数:在 API 请求期间很重要,以限制要检索的数据。
- ...
当我在 Effect 中时,如何直接检索我的重要参数以启动我的 API 请求?
我知道要恢复数据,有必要使用 "selectors" 来获得一个 "Observable",这将使我在更改到达时立即得到通知。
然而,在这里我想要现在的值而不用等待启动我的 API 请求。
我该怎么办?
提前致谢
正如您正确指出的那样,您应该使用 选择器 从您的商店访问数据。您可以在组件中使用选择器,也可以在效果中使用。
看这个例子效果:
@Injectable()
export class ElementEffects {
constructor (private store: Store<any>, private apiService: ApiService) {}
@Effect()
public yourEffect: Observable<Action> = this.actions$.pipe(
ofType<yourActionClass>(ActionsEnum.YOUR_ACTION),
withLatestFrom(this.store.pipe(select(selectSomethingFromTheStore))),
concatMap(([action, selectedDateFromTheStore]) => this.apiService.doBackendCall(selectedDateFromTheStore, action.payload).pipe(
map(([resultFromTheBackendCall, selectedDateFromTheStore]) => {
// Do Stuff
},
catchError((error) => of(new FailureAction(error)))
)
),
);
}
"withLatestFrom" 允许您添加另一个可观察对象 到您的链。您将立即得到结果并可以使用它。
https://www.learnrxjs.io/operators/combination/withlatestfrom.html
我开始使用 NgRx,但对访问 State 中包含的数据有点迷茫。 在问我的问题之前,我搜索了几个小时,因为我没有真正找到答案。
当使用 NgRx 将数据显示在列表中时,它很棒。 我想更多地使用 NgRx 将我的应用程序设置与我的 SettingsState 分组。
我想在我的 SettingsState 中特别说明: - API 请求的基础 URL, - 列表中显示的行数:在 API 请求期间很重要,以限制要检索的数据。 - ...
当我在 Effect 中时,如何直接检索我的重要参数以启动我的 API 请求?
我知道要恢复数据,有必要使用 "selectors" 来获得一个 "Observable",这将使我在更改到达时立即得到通知。
然而,在这里我想要现在的值而不用等待启动我的 API 请求。 我该怎么办?
提前致谢
正如您正确指出的那样,您应该使用 选择器 从您的商店访问数据。您可以在组件中使用选择器,也可以在效果中使用。
看这个例子效果:
@Injectable()
export class ElementEffects {
constructor (private store: Store<any>, private apiService: ApiService) {}
@Effect()
public yourEffect: Observable<Action> = this.actions$.pipe(
ofType<yourActionClass>(ActionsEnum.YOUR_ACTION),
withLatestFrom(this.store.pipe(select(selectSomethingFromTheStore))),
concatMap(([action, selectedDateFromTheStore]) => this.apiService.doBackendCall(selectedDateFromTheStore, action.payload).pipe(
map(([resultFromTheBackendCall, selectedDateFromTheStore]) => {
// Do Stuff
},
catchError((error) => of(new FailureAction(error)))
)
),
);
}
"withLatestFrom" 允许您添加另一个可观察对象 到您的链。您将立即得到结果并可以使用它。
https://www.learnrxjs.io/operators/combination/withlatestfrom.html