如何在管道异步和组件中使用对象?
我需要两次使用可观察的参考。 在HTML中:
<div *ngIf="subject$ | async as subject">
在模板中:
this.subject = subject
I need to use the reference of observable twice.
in html:
<div *ngIf="subject$ | async as subject">
and in template:
this.subject = subject
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
.pipe> .pipe()
将数据, 然后使用tap()
将值提取到组件中存在的变量中。You can use a
.pipe()
to pipe the data, then usetap()
to extract the value into a variable that exists within the component.如果您想获得代码中的值,
我想您需要订阅,这是您的进程;但是,Alias
作为主题
仅在标签内部存在。您应该使用
share() >确保您不会触发两次的东西(例如服务器请求):
其他好处是
可观察
现在是可观察到的
无法在“消耗代码”上发出新值。If you want to get the value in you code, you need to subscribe
I guess, that this was your intenion; but the alias
as subject
only exists inside the tag it was defined.You should use the
share()
Operator to ensure you do not trigger something twice (like a server request):additional benefit is that
observable
is now anObservable
which can not be used to emit a new value at the "consuming code".希望该解决方案可以帮助您了解如何使用可观察的两次 https://stackblitz.com/edit/angular-ivy-jgcgnf?file=src/app/app/app/app.component.ts
Hopefully, this solution can help you to understand how to use observable twice https://stackblitz.com/edit/angular-ivy-jgcgnf?file=src/app/app.component.ts