服务
mode: Subject<any> = new Subject<any>();
constructor(public http: HttpClient) {
this.setMode({ // defaults
admin: this.admins.includes(localStorage.getItem("email")),
edit: false,
dev: false
});
}
getMode() {
return this.mode.asObservable();
}
setMode(value) {
this.mode.next(value);
}
零件
constructor(private bcAuthService: BcAuthService) {}
ngOnInit() {
this.bcAuthService.getMode().subscribe(mode => {
console.log('mode: ', mode); // never logs or prints in the template
this.mode = mode;
});
}
editToggle(e) {
this.mode.edit = e.target.checked; // err: cant edit `mode` because it never set.
this.bcAuthService.mode.next(this.mode);
}
问题
我正在尝试设置一个可从许多组件读取和写入的 Observable。如上所示;我在我的服务中设置了 observable,但我的组件getMode()
不起作用。任何想法为什么?