Observable vs asObservable()?

风格不统一 提交于 2020-01-13 02:39:08

问题


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();

回答1:


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

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



来源:https://stackoverflow.com/questions/42272821/observable-vs-asobservable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!