简体   繁体   English

如何在定义观察者时在第一次发射时取消订阅 Observable/Subject

[英]How can I unsubscribe from Observable/Subject on the first emission while defining the observer

I am working on some dialogs with angular 9 and I came into a situation where I need to unsubscribe from a Subject after the first emission.我正在使用 angular 9 处理一些对话框,我遇到了需要在第一次发射后取消订阅主题的情况。

Initially, I thought of piping the subject using the take(1) or first() operator and subscribe to this "transformed finite subject".最初,我想使用take(1)first()操作符来传递主题并订阅这个“转换的有限主题”。 So the unsubscription will be handled automatically just after the first emissions ocurrs, because when an observable completes the unsubscription logic is called.所以取消订阅将在第一次发射后自动处理,因为当一个可观察对象完成时,取消订阅逻辑被调用。 Example:例子:

const subject = new Subject();

subject.pipe(take(1)).subscribe(console.log);

subject.next("hola");

But then I have also found this "hack" that I think its also working, can someone confirm?但是后来我也发现了这个我认为它也有效的“黑客”,有人可以确认吗?

const subject = new Subject();

const subscription = subject.subscribe((value) => {
  console.log(value);
  subscription.unsubscribe(); // <----- this is the magic line
}

subject.next("hola");

I have never seen this before, I think its convenient in some occasions.我以前从未见过这个,我认为在某些场合它很方便。 The thing that confused me is that the observer is calling the subscription defined in the same line.让我感到困惑的是观察者正在调用同一行中定义的订阅。 Is this good practice?这是好习惯吗? Is there any other way of achieving this?有没有其他方法可以实现这一目标?

Thanks!谢谢!

You can use first() or take(1) .您可以使用first()take(1) both unsubscribe automatically when their condition is met.当满足条件时,两者都会自动取消订阅。

const { Subject } = require('rxjs/');
const { take, first } = require('rxjs/operators');

const subject = new Subject();

subject.pipe(first()).subscribe(data => {
  console.log({ data });
});

subject.pipe(take(1)).subscribe(data => {
  console.log({ data });
});

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

相关问题 如何订阅和取消订阅可观察对象? - How can I subscribe and unsubscribe from an observable? 我应该取消订阅从主题派生的 Observable 吗? - Should I unsubscribe from an Observable derived from a Subject? 如何让 RxJS 可观察管道访问原始 observable 的发射和管道的先前发射? - How can I give an RxJS observable pipe access to the original observable's emission AND the pipe's previous emission? 在第一个值发射后,对象从数组中失去观察者。 Angular RouteReuseStrategy table-edit form 通讯 - Subject loses observer from array after first value emission. Angular RouteReuseStrategy table-edit form communications 如何取消订阅嵌套的 observable - How to unsubscribe from nested observable 如何取消订阅RxJS 5可观察? - How to unsubscribe from an RxJS 5 observable? 注销后如何取消订阅主题? 我的用户实际上是我的主题 - How must I unsubscribe from a subscription of subject after logout? My user is actually my subject 我是否需要取消订阅 Angular Observable? - Do I need to unsubscribe from an Angular Observable? 我应该取消订阅 Cold Observable 吗? - Should I unsubscribe from Cold Observable? 我应该取消订阅组件中定义的 observable 吗? - Should I unsubscribe from observable defined in component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM