嗨,所有人,并感谢您的帮助提前... 我正在建立一个应用程序,我想有一个服务,是用来处理一些配置和它由不同的模块共享。 我真的不知道这是否是最好的方法,但我使用这个,因为angularJS。角2和rxjs服务主体没有正在更新组件
配置服务
sidebarCompress: Subject<boolean> = new Subject<boolean>();
public sidebarCompressed: Observable<boolean> = this.sidebarCompress.asObservable();
constructor() {}
compressSidebar(val: boolean = false) {
this.sidebarCompress.next(val);
// this is firing when used
console.log('changed on service');
}
边栏组件
constructor(
private conf: ConfigService,
) {
conf.sidebarCompressed.subscribe(
sidebarCompressed => {
// this is not firing
console.log('changed on sidebar component');
});
}
任何其它组分
constructor(
private conf: ConfigService,
) { }
ngOnInit() {
// The idea is to use it in a fn (not always on init)
this.conf.compressSidebar(true);
}
所以,边栏组件订阅不起作用,任何人都知道我在这里做错了什么,或者如果我应该采取不同的做法!?
再次,非常感谢。
你在哪里提供'ConfigService'? –
'ConfigService'在每个模块中提供。 在这种情况下,我有模块,我有'SidebarComponent'和一个懒惰的加载模块调用与试图压缩侧边栏的组件。 这两个模块都提供了'ConfigService' ... 我也提供'app.module'以及... –