简体   繁体   English

在rxjs中的subscribe功能块内部处理/取消订阅

[英]dispose/unsubscribe inside subscribe function block in rxjs

I've a subject called authenticated in _auth service. 我在_auth服务中有一个名为authenticated主题

I want to dispose/unsubscribe it if some condition fails so that this subscribe doesn't call when next value arrives in authenticated subject. 如果某些条件失败 ,我想处理/取消订阅以便当下一个值到达经过身份验证的主题时,此订阅不会调用。

                this.sub = this._auth.authenticated
                .subscribe((loggedIn) => {
                    if (!loggedIn) {
                        this.sub.dispose(); // doesn't work because this.sub is null
                        this._location.replaceState('/');
                        this._auth.doLogout(); // emits value again in authenticated subject
                    }
                });

What is the rxjs-5 way of doing it? rxjs-5的做法是什么?

你持有对可观察的Eg:obs $的引用并且调用obs $ .unsubscribe();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM