节点BehaviorSubject问题仅获取空对象

发布于 2025-01-10 03:24:30 字数 448 浏览 2 评论 0原文

我正在使用 Angular 版本 7 并尝试使用 BehaviorSubject 但是当我获得该值时,我总是收到一个空对象

valueempty

line: 315 | let value: any = await this._service.machine$.pipe(take(1)).toPromise()

Emmit

this._service.emitterMachineSelected('new')

服务代码

I am using Angular version 7 and trying to use a BehaviorSubject
but when I get the value, all times I receive an empty object

value empty

line: 315 | let value: any = await this._service.machine$.pipe(take(1)).toPromise()

Emmit

this._service.emitterMachineSelected('new')

code of service

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻拂→两袖风尘 2025-01-17 03:24:30

_machine 的默认值

_machine 是一个包含空对象的 BehaviorSubject(首先)。 ({} 是一个空对象)。这就是您阅读以下行的方式:

_machine = new BehaviorSubject({});

因此,当您获取该值时,您将得到一个空对象(BehaviorSubject 的默认值)。


使用 .next 发出一个值

一段时间后,它包含字符串 "new"

this_machine.next("new");

您没有在任何地方获取此值,因此您不应期望看到它任何地方。

_machine's default value

_machine is a BehaviorSubject that (to start with) contains an empty object. ({} is an empty object). That's how you read the following line:

_machine = new BehaviorSubject({});

So when you grab that value, an empty object (The default value of the BehaviorSubject) is what you get.


Using .next to emit a value

Some time later, it contains the string "new"

this_machine.next("new");

You're not grabbing this value anywhere, so you shouldn't expect to see it anywhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文