Angular 4共享服务可观察到的行为主题 [英] Angular 4 Shared Service Observable Behavior Subject
问题描述
我有一个BehaviorSubject作为SharedServices模块的一部分,该模块在Providers部分中提供给AppModule,并在AppComponent中进行订阅.我还在另一个模块AdminModule的另一个组件中订阅了它,但是除非将其添加到AppComponent的Providers部分中,否则我不会获得更改.为什么是这样?我以为如果将其添加到模块"的提供者"部分并订阅所有订阅者,将获得更改?
我必须对子组件执行此操作才能获取更改.我对此感到非常困惑.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
providers : [ AppsSharedService] <--------
您可能要遵循Angular文档的指导,然后将此全局提供程序从该共享"模块移至核心"模块:>
https://angular.io/guide/ngmodule#no-shared -module-providers
我怀疑这里发生的事情是该服务创建了两次,因此一次发生的更改不会全部反映出来.
I have an BehaviorSubject as part of a SharedServices Module that is provided to the AppModule in the Providers section of the module and subscribe to it in the AppComponent. I also subscribe to it in another component that is part of another Module AdminModule but I'm not getting the changes unless I add it to it Providers section of the AppComponent. Why is this? I thought if you add it to the Providers section in the Module and subscribe all subscribers would get the changes?
I have to do this for the child components to get the changes. I'm really confused by this.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
providers : [ AppsSharedService] <--------
You might want to follow the guidance of the Angular docs and move this global provider out of that "shared" module and into a "core" module:
https://angular.io/guide/ngmodule#no-shared-module-providers
I suspect what's happening here is the service is created twice, so changes that are happening in one aren't reflected in all.
这篇关于Angular 4共享服务可观察到的行为主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!