Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.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
Javascript 向可观察对象发出新值_Javascript_Angular_Rxjs - Fatal编程技术网

Javascript 向可观察对象发出新值

Javascript 向可观察对象发出新值,javascript,angular,rxjs,Javascript,Angular,Rxjs,我有以下代码: this.isUsingGoogleTwoFactor$ = this.user$.pipe(map((user: User) => user.isUsingGoogleTwoFactor)); 现在我想要的是使用GoogleTwoFactor$向发出新值。我知道我不能用观测值来做这件事,但我应该用主题。但我不知道如何使用管道 谢谢 检查。它是一种可观察的,允许你推送新的值,并跟踪它发出的最后一个值 我链接的那个网站就是如何使用它的一个例子。但简而言之,这就是你如何使用

我有以下代码:

this.isUsingGoogleTwoFactor$ = this.user$.pipe(map((user: User) => 
user.isUsingGoogleTwoFactor));
现在我想要的是使用GoogleTwoFactor$向
发出新值。我知道我不能用
观测值来做这件事
,但我应该用
主题
。但我不知道如何使用管道

谢谢

检查。它是一种可观察的,允许你推送新的值,并跟踪它发出的最后一个值

我链接的那个网站就是如何使用它的一个例子。但简而言之,这就是你如何使用它:

const observable = new BehaviorSubject("initial value");
如果此时出现订户,并执行:

observable.subscribe({
    next: (value) => console.log("The value is: ", value)
});
那么输出将是:

The value is: initial value
此后,每次调用
next
时,所有订户都将收到通知:

observable.next("a new value");
将在订阅服务器中打印此内容:

The value is: a new value

如果我理解正确,您希望在用户$发生更改时以及手动触发时触发此可观察项。然后你可以用这种方法

import { merge, Subject } from 'rxjs';

...

const manuallyTriggered$ = new Subject();
const triggeredFromUser$ = this.user$.pipe(
    map((user: User) => user.isUsingGoogleTwoFactor)
);

this.isUsingGoogleTwoFactor$ = merge(manuallyTriggered$, triggeredFromUser$);
当您想要手动触发它时:

manuallyTriggered$.next(true);

请看@Rafael Yep看到了这一点,但我如何将其用于管道?我如何将其用于管道?您已经拥有了它,在我的示例中,我将主题称为“可观察的”
,在您的示例中,您将其称为“this.user$”。因此,无论你的应用程序生成了
用户$
可观察对象的哪个部分,都应该使用
行为主体
我会检查这个。嗨,我试过这个:this.isUsingGoogleTwoFactor$=merge(this.isUsingGoogleTwoFactorSubject,this.user$.pipe(mergeMap((user:user)=>of(user.isUsingGoogleTwoFactor));但是我得到了一个错误:类型“OperatorFunction”缺少类型“Observable”中的以下属性:\ isScalar、source、operator、lift等6个。我使用了BEhaviorSubject。但你的解决方案是有效的。非常感谢。很高兴听到:)