【发布时间】:2020-03-20 12:04:55
【问题描述】:
我正在尝试向 BehaviorSubject 添加一个布尔值。但是,当我尝试控制台日志时,它说它是未定义的。我做错了什么?
这就是我的声明方式。
isDesktopWidth: BehaviorSubject<boolean>;
这就是我使用它的方式
changeSideBarWidth() {
if (this.innerWidth > 767) {
this.isDesktopWidth.next(true);
} else {
this.isDesktopWidth.next(false);
}
}
然后在 ngOnInit 中
this.changeSideBarWidth();
console.log(this.isDesktopWidth.value);
但它不会在控制台中显示任何内容。只有一个错误说
无法读取未定义的属性“下一个”
【问题讨论】:
标签: angular observable behaviorsubject