简体   繁体   English

Rxjs - 组合2个Observable并发出组合结果不起作用

[英]Rxjs - Combine 2 Observables and emit combined results not working

First off, here are my import statements for rxjs: 首先,这是我对rxjs的import语句:

import { Subject, Observable, merge, combineLatest } from "rxjs";
import { map } from 'rxjs/operators';

Here is my rxjs version in package.json: "rxjs": "^6.5.2" 这是我在package.json中的rxjs版本: "rxjs": "^6.5.2"

I have an Observable ( this.searchResults$ ) which emits the results of a search API request. 我有一个Observable( this.searchResults$ ),它会发出搜索API请求的结果。 I now want to do another search and combine the new search results with the old search results, and put them all in this.searchResults$ . 我现在想要进行另一次搜索并将新的搜索结果与旧的搜索结果相结合,并将它们全部放在this.searchResults$ this.handleSearch() returns the observable with the search results this.handleSearch()返回带有搜索结果的observable

I thought I would do this: 我以为我会这样做:

const newSearchResults$ = this.handleSearch(undefined, this.currentPage + x);
merge(newSearchResults$, this.searchResults$).subscribe(x => console.log(x));

But this console logs: 但是这个控制台记录:

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
(6) [{…}, {…}, {…}, {…}, {…}, {…}]

The second line of the console log needs to contain 16 items. 控制台日志的第二行需要包含16个项目。 Instead it gets overwritten with the new 6 items. 相反,它会被新的6项覆盖。

I looked online why merge does not combine the results and found that I should use combineLatest : 我在网上看到为什么合并没有结合结果,发现我应该使用combineLatest

const newSearchResults$ = this.handleSearch(undefined, this.currentPage + x);
newSearchResults$.subscribe(x => x);
const combinedSearchResults$ = combineLatest(newSearchResults$, this.searchResults$)
    .pipe(map(([s1, s2]: Array < any > ) => [...s1, ...s2]));
combinedSearchResults$.subscribe(x => console.log(x));

This does not console.log anything at all and has no errors. 这根本没有console.log任何东西,没有错误。 What am I doing wrong? 我究竟做错了什么?

As per Pavel's answer I tried: 按照帕维尔的回答我试过:

const newSearchResults$ = this.handleSearch(undefined, this.currentPage + x);
this.searchResults$.pipe(
    mergeMap((d1: any) => newSearchResults$.pipe(map(d2 => [...d1, ...d2])))
).subscribe(console.log);

I can't get anything to console.log with this either. 我也无法得到任何与console.log相关的东西。

If you wan't to merge 2 observables you can use mergemap operator. 如果你不想合并2个observable,你可以使用mergemap运算符。 try something like this: 尝试这样的事情:

var obs1 = lastData;
var obs2 = newData;

obs1.pipe(
    mergeMap(d1 => obs2.pipe(map(d2 => [...d1, ...d2])))
).subscribe(console.log);

Secondly be aware if you use cold or hot observable, because if you use the cold one the value won't be saved and the API will be called each time you subscribe to it. 其次要注意,如果你使用冷或热可观察,因为如果你使用冷的,那么不会保存该值,每次订阅时都会调用API。 (you can create hot observable from cold with shareReplay operator. (您可以使用shareReplay运算符从cold创建hot observable。

Or you can use BehaviorSubject like in this example: https://stackblitz.com/edit/angular-dkh5ji 或者您可以像本例中一样使用BehaviorSubject: https//stackblitz.com/edit/angular-dkh5ji

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RxJS。 组合发射时间小于 1 秒的可观察对象 - RxJS. Combine observables that emit less then 1 second apart RxJS 仅当第一个(源)observable 发出一个值而不是其他组合的 observable 时才发出一个值 - RxJS Emit a value only if the first (source) observable emits a value and not other combined observables RxJS combineLatest 无需等待源 observables 发出? - RxJS combineLatest without waiting for source observables to emit? 如何组合可观察对象,但在每个对象发生时发出一个值 - How to combine observables but emit a value for each as it occurs RxJS:使用更多/更少的Observables更新`combineLatest`以进行组合 - RxJS: Updating a `combineLatest` with more/less Observables to combine 如何将多个 rxjs observables 与单个订阅结合起来? - How to combine multiple rxjs observables with a single subscription? RxJS:如何将多个嵌套的observable与缓冲区组合在一起 - RxJS: How to combine multiple nested observables with buffer 将rxjs中的多个observable组合到一个缓冲区中 - Combine multiple observables from rxjs into a single buffer 将多个 observable 组合成一个 RxJS 流 - Combine multiple observables in to a single RxJS stream Angular Rxjs:使用异步管道连续延迟发出所有合并的可观察对象 - Angular Rxjs: emit all merged observables with delay continuously with async pipe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM