传递给 RXJS 中的 switchMap() 时对象数组的奇怪行为
Strange behaviour of an array of objects when passed into switchMap() in RXJS
我有一个简单的目标,即创建一个可以发出世界上所有国家/地区列表的可观察对象。 observable 的类型是 Country[]
,即存储值被类型化为一个对象数组。但是,当传递给 switchMap
时,它会神奇地转换为类型 Country
。我不知道是什么导致了这种行为,如果不是偶然的话,就在我这边。这是有问题的功能:
public getAllCountries(): Observable<Country[]> {
const getAppState = createFeatureSelector<ServicesState>('services');
const getCountries = createSelector( getAppState, (state: ServicesState): Country[] => state.countries);
// as you can see, this is the NGRX store slice that must be of type Observable<Country[]>
const countriesFromStore$ = this.store.pipe(select(getCountries));
return countriesFromStore$.pipe(
// this is the switchMap that causes the error
switchMap( countries => {
// this is the return statement that somehow converts an array of objects into a single object??
if (countries) { return countries; }
const countriesFromServer$: Observable<Country[]> = this.http.get<FilteredArrayResponse<Country>>
(this.baseUrl, this.config.httpGetJsonOptions).pipe(
tap( result => this.store.dispatch( new LoadCountriesAction(result.data)) ),
map( result => result.data)
);
return countriesFromServer$; })
);
}
如果您有任何疑问:
FilteredArrayResponse
是具有 data
属性的通用接口,实际上包含数组
- 完整的错误信息是
Type 'Observable<Country | Country[]>' is not assignable to type 'Observable<Country[]>'.
- 我的猜测是
switchMap
以某种方式混淆了 Observable of Arrays 和 Array of Observables...
result.data
的return类型总是数组
map
家族的其他运算符表现出相同的行为
尝试在 switchMap
(import { of } from 'rxjs';
) 中返回 of(countries)
。我认为应该解决它。 switchMap
需要切换到 Observable
。
import { of } from 'rxjs';
...
switchMap((countries: Country[]) => {
if (countries) {
return of(countries);
}
....
})
我有一个简单的目标,即创建一个可以发出世界上所有国家/地区列表的可观察对象。 observable 的类型是 Country[]
,即存储值被类型化为一个对象数组。但是,当传递给 switchMap
时,它会神奇地转换为类型 Country
。我不知道是什么导致了这种行为,如果不是偶然的话,就在我这边。这是有问题的功能:
public getAllCountries(): Observable<Country[]> {
const getAppState = createFeatureSelector<ServicesState>('services');
const getCountries = createSelector( getAppState, (state: ServicesState): Country[] => state.countries);
// as you can see, this is the NGRX store slice that must be of type Observable<Country[]>
const countriesFromStore$ = this.store.pipe(select(getCountries));
return countriesFromStore$.pipe(
// this is the switchMap that causes the error
switchMap( countries => {
// this is the return statement that somehow converts an array of objects into a single object??
if (countries) { return countries; }
const countriesFromServer$: Observable<Country[]> = this.http.get<FilteredArrayResponse<Country>>
(this.baseUrl, this.config.httpGetJsonOptions).pipe(
tap( result => this.store.dispatch( new LoadCountriesAction(result.data)) ),
map( result => result.data)
);
return countriesFromServer$; })
);
}
如果您有任何疑问:
FilteredArrayResponse
是具有data
属性的通用接口,实际上包含数组- 完整的错误信息是
Type 'Observable<Country | Country[]>' is not assignable to type 'Observable<Country[]>'.
- 我的猜测是
switchMap
以某种方式混淆了 Observable of Arrays 和 Array of Observables... result.data
的return类型总是数组map
家族的其他运算符表现出相同的行为
尝试在 switchMap
(import { of } from 'rxjs';
) 中返回 of(countries)
。我认为应该解决它。 switchMap
需要切换到 Observable
。
import { of } from 'rxjs';
...
switchMap((countries: Country[]) => {
if (countries) {
return of(countries);
}
....
})