BehaviorSubject is executed more than once

心不动则不痛 提交于 2019-12-02 06:22:51

You probably forgot to unsubscribe in your StudentComponent when the components gets destroyed. Save the subscription in a class variable and in ngOnDestroy() lifecycle hook call subscription.unsubscribe()

This will call twice, because whenever the behaviourSubject changes it will emit the latest values. Previously it was null (as default value) which you have given it and next when the service response came you are doing next so it will emit the latest value after change, which is fair enough. For your code you should only check for

if(value){// do the stuff here}

Try a replay subject that has no initial value but will still cache your stream:

export class Globals {
loadStudentsByClass = new ReplaySubject<string>(1);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!