本文介绍了可观察与asObservable()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular2的新手,我只是好奇地知道,如果我在_showNavBarshowNavBarEmitter上进行订阅,两者的工作原理相同(请参见下面使用的代码).有什么区别吗?

I'm new to Angular2 and I'm just curious to know That If I do a subscribe on _showNavBar or on showNavBarEmitter both works same(see below code i'm using). is there any difference?

public _showNavBar: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
public showNavBarEmitter: Observable<boolean> = this._showNavBar.asObservable();

推荐答案

asObservable使订阅者无法访问原始主题.这样,您可以限制谁只能订阅,谁也可以发出值.

asObservable makes the original subject inaccessible for subscribers. This way you can limit who can only subscribe and who can also emit values.

要使此方法生效,您需要制作_showNavBar private.

For this to take effect you would need to make _showNavBar private though.

这篇关于可观察与asObservable()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 00:39