【发布时间】:2021-05-20 23:58:42
【问题描述】:
我有一个 Angular 11 应用程序,它将各种数据保存在 RxJS 存储中。当组件需要访问该数据时,它会这样调用它:
this.store.select('myStore').subscribe(t => {
this.array1 = t.array1;
this.array2 = t.array2;
this.array3 = t.array3;
this.formData = t.myForm;
});
最后一条信息是存储形式。它只包含一堆值:
export interface IOpportunitySearch {
fromDate: Date,
toDate: Date,
selectedUserIds: number[],
...
userName: string
}
我想要做的是,当 myForm 数据从存储中返回时,设置一个回调来填充此组件中的表单。
有什么方法可以在检索到数据后触发填充表单?
有一个函数可以使用加载后我需要运行的数据进行 API 调用。该函数使用存储中的数据进行 api 调用:
updateData(form: IOpportunitySearch){
...
this.storeService.onSetAllOpportunitiesSearch(form); //this sets the store with the user's selected properties
this.dataService.searchOpportunities(form).subscribe(data => {
所以当组件最初加载时,包括来自商店的表单数据,我想调用这个 updateData 方法。
【问题讨论】:
-
这里有什么问题?为什么不能在
subscribe()方法中这样做? -
加载数据后,我想使用该数据进行后续的 ajax 调用。它加载了我想发送到 api 的各种搜索参数
-
有几种方法可以做到这一点,您应该为您的问题添加更多详细信息。您可以直接从
subscribe回调中进行调用,或者(更好地)使用 RxJS 运算符链接您的 http 调用。 You can easily find several answers on SO about that -
使用
switchMap运算符的示例:callApiOne().pipe(switchMap(dataFromApiOne => {return callApiTwo(dataFromApiOne);})).subscribe(dataFromApiTwo => {...}) -
但是按照示例,这也适用于 rxjs 商店吗?