我遇到的情况是我正在对Laravel后端进行API调用并且急于加载一些数据。数据是一组位置,每种形式都是:
location = {
id: ..
name: ...
...
docks: [
{
id: ...
name: ...
},
...
]
}
这个史诗的最终结果应该是发送2个动作,一个带有位置列表,另一个带有所有相关码头的列表
从对api调用的响应中获取它,以下引发了Typescript错误并且不会编译:
.mergeMap(({ response }) => {
if (response.messages) {
return Observable.of(GetLocationsForReceiverFailedAction(response.messages))
}
const locations = response.locations.map((locn: any) => ({
id: locn.id,
receiver_id: locn.receiver_id,
name: locn.name,
address: locn.address,
city: locn.city,
state: locn.state,
zip: locn.zip
}))
const location$ = Observable.of(GetLocationsForReceiverSuccessAction(locations))
const docks: any[] = []
response.locations.forEach((locn: any) => {
locn.docks.forEach((dk: any) => {
docks.push(dk)
})
})
const docks$ = Observable.of(GetDocksForReceiverSuccessAction(docks))
return Observable.merge(location$, docks$)
})
.catch(() =>
Observable.of(
GetLocationsForReceiverFailedAction([
'Something went wrong trying to reach the server'
])
)
)
错误如下: “类型的参数”({response}:AjaxResponse)=> Observable< {type:ActionTypes; messages:string [];}> | Observab ...'不能赋值给'类型'的参数(值:AjaxResponse, index:number)=> ObservableInput< {type:ActionTypes; messages:string [];}>'。“
但是,如果我更改了这一行:
return Observable.merge(location$, docks$)
为:
return Observable.merge(location$, Observable.empty(), docks$)
(即,通过合并中的空observable将2个可观察对象分开(或者concat,在这里以相同的方式工作),它编译并运行。
我在这里不理解什么?