【发布时间】:2017-02-18 13:04:24
【问题描述】:
大家好,提前感谢您的帮助... 我正在构建一个应用程序,我想要一个用于处理某些配置的服务,它由不同的模块共享。 我真的不知道这是否是最好的方法,但我从 angularJS 开始使用它。
配置服务
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中提供了它...
标签: javascript angular typescript observable subject