const input$ = fromEvent(textInput, 'keyup');
input$.pipe(
map(event => {
const term = event.target.value;
return ajax.getJSON(`https://api.github.com/users/${term}`) // return a nested observable
}),
debounceTime(200),
).subscribe((obs) => {
obs.subscribe(console.log) // you have to subscribe again
})
One way can solve the problem is mergeAll
: it will help to subscribe nested observable
const input$ = fromEvent(textInput, 'keyup');
input$.pipe(
map(event => {
const term = event.target.value;
return ajax.getJSON(`https://api.github.com/users/${term}`) // return a nested observable
}),
debounceTime(200),
mergeAll(),
).subscribe(console.log)
It is so common map + mergeAll
, so can use mergeMap()
:
const input$ = fromEvent(textInput, 'keyup');
input$.pipe(
debounceTime(200),
mergeMap(event => {
const term = event.target.value;
return ajax.getJSON(`https://api.github.com/users/${term}`) // return a nested observable
}),
).subscribe(console.log)
标签:term,const,mergeMap,return,subscribe,mergeAll,RxJS,input,event From: https://www.cnblogs.com/Answer1215/p/16804233.html