【发布时间】:2019-02-04 08:30:45
【问题描述】:
我有一个可观察的userInput$,当用户键入要输入时,它每 N 秒返回一次数据流。我想接收最新的输入并将其作为参数传递给函数service.search(x),该函数将返回另一个带有数据列表的可观察对象。
this.userInput$
.pipe(withLatestFrom(x => this.service.search(x)))
.subscribe(x => {
//do not receiving any data here
});
为什么我的代码不起作用?
我的userInput$ 返回一个string 我的this.service.search(x) 返回一个数组——这就是我想要得到的结果。
更新:
const example = this.userInput$
.pipe(withLatestFrom(x => this.service.search(x)),
map(([userInput, searchOutput ]) => { // error here
return {query: userInput, result: searchOutput};
})
);
[userInput, searchOutput] - 收到错误[ts] Type 'Observable<GetSearch[]>' is not an array type. [2461]
仅用于测试将this.service.search(x) 更改为of(x)
const example = this.userInput$
.pipe(withLatestFrom(x => of(x)),
map(([userInput, searchOutput ]) => { // error here
return {query: userInput, result: searchOutput};
})
);
map 返回错误[ts] Type 'Observable<any>' is not an array type. [2461]
【问题讨论】:
-
userInput$ 返回什么? & search(x) 返回什么?您对订阅有何期待?