Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs 如何将冷变热?_Rxjs_Observable - Fatal编程技术网

Rxjs 如何将冷变热?

Rxjs 如何将冷变热?,rxjs,observable,Rxjs,Observable,我似乎无法将这种冷的观察转变为热的观察: const test = new BehaviorSubject('test').pipe(tap(() => console.log('I want this to be logged only once to the console!'))) const grr = test.pipe( share(), // share() seems to not do anything take(1), // The culprit is he

我似乎无法将这种冷的观察转变为热的观察:

const test = new BehaviorSubject('test').pipe(tap(() => console.log('I want this to be logged only once to the console!')))

const grr = test.pipe(
  share(), // share() seems to not do anything
  take(1), // The culprit is here, causes logging to take place 5 times (5 subscribers)
  share() // share() seems to not do anything
)

grr.subscribe(() => console.log(1))
grr.subscribe(() => console.log(2))
grr.subscribe(() => console.log(3))
grr.subscribe(() => console.log(4))
grr.subscribe(() => console.log(5))

// Expected output:
// 'I want this to be logged only once to the console!'
// 1
// 2
// 3
// 4
// 5

我应该如何更改此选项以生成所需的输出?

您可以使用
publishReplay
refCount
运算符,如下所示:

import { interval, BehaviorSubject } from 'rxjs';
import { publishReplay, tap, refCount } from 'rxjs/operators';

const test = new BehaviorSubject('test').
                  pipe(
                    tap(() => console.log('I want this to be logged only once to the console!')
                    ),
                    publishReplay(1),
                    refCount()
                  );

test.subscribe(() => console.log(1));
test.subscribe(() => console.log(2));
test.subscribe(() => console.log(3));
test.subscribe(() => console.log(4));

工作Stackblitz:

您可以使用
publishReplay
refCount
操作符,如下所示:

import { interval, BehaviorSubject } from 'rxjs';
import { publishReplay, tap, refCount } from 'rxjs/operators';

const test = new BehaviorSubject('test').
                  pipe(
                    tap(() => console.log('I want this to be logged only once to the console!')
                    ),
                    publishReplay(1),
                    refCount()
                  );

test.subscribe(() => console.log(1));
test.subscribe(() => console.log(2));
test.subscribe(() => console.log(3));
test.subscribe(() => console.log(4));

工作堆栈闪电战:

测试
已经很热了。一个
行为主体
——通过设计——将其当前价值发送给新订户。如果您不想这样做,请改用
主题
。我需要它在其他地方成为行为主题,但这里我想转换它,并尝试使用share()。通常,share()在许多情况下都适用于我。共享使用引擎盖下的主题。
跳过(1)
如果您不需要当前值。这不是我需要的。我需要运行subscribe函数,但是控制台日志只发生一次。也就是说,我需要多个观察员,事件只发生一次@cartant
test
已经很热了。一个
行为主体
——通过设计——将其当前价值发送给新订户。如果您不想这样做,请改用
主题
。我需要它在其他地方成为行为主题,但这里我想转换它,并尝试使用share()。通常,share()在许多情况下都适用于我。共享使用引擎盖下的主题。
跳过(1)
如果您不需要当前值。这不是我需要的。我需要运行subscribe函数,但是控制台日志只发生一次。也就是说,我需要多个观察员,事件只发生一次@cartantHow这与shareReplay()不同吗?您可以出于同样的目的使用
shareReplay
[我也曾使用过它,直到我在rxjs 6.2.2版中发现一个bug;但是请记住,
shareReplay
即使您取消订阅,也会保持订阅运行[至少在6.2.2版中]。我已经更新了相同的stackblitz以显示
shareReplay
的行为。我没有在最新版本上测试
shareReplay
(需要使用一些配置)。为了避免内存泄漏,我开始使用
publishReplay(1),refCount()
@user2216584这是固定的,因此
shareReplay({bufferSize:1,refCount:true})
也会这样做,请参阅@OlesSavluk谢谢您让我知道!这与shareReplay()有什么不同?您可以出于相同的目的使用
shareReplay
[在rxjs版本6.2.2中发现一个bug之前,我也一直使用它;但请记住,
shareReplay
即使您取消订阅,也会保持订阅运行[至少在版本6.2.2中]。我已经更新了相同的stackblitz以显示
shareReplay
的行为。我没有在最新版本上测试
shareReplay
(需要使用一些配置)。为了避免内存泄漏,我开始使用
publishReplay(1),refCount()
@user2216584这是固定的,因此
shareReplay({bufferSize:1,refCount:true})
也会这样做,请参阅@OlesSavluk谢谢您让我知道!