简体   繁体   English

Angular 2:如何更新模块组件中的服务变量,以便惰性模块中的变量自动更新?

[英]Angular 2: how to update service variable in a module's component so that the variable in lazy module updates automatically?

Below is a link to project on Plunker. 以下是Plunker项目的链接。 where there are two components eager and lazy, also there is shared service which is used on both components. 哪里有两个组件渴望和懒惰,也有共享服务,在两个组件上使用。

How to update service variable in a module's component so that the variable in lazy module updates automatically? 如何更新模块组件中的服务变量,以便惰性模块中的变量自动更新?

[Plunker example][1]
[1]: https://plnkr.co/edit/L2ypUQZiltSPXnLlxBoa?p=preview

You have this behavior because of child injector created new instance of the service. 您有这种行为是因为子注入器创建了新的服务实例。 For creating one instance of the service for application, you should use .forRoot() method 要为应用程序创建一个服务实例,您应该使用.forRoot()方法

 import { NgModule, ModuleWithProviders } from '@angular/core'; import { CommonModule } from '@angular/common'; import { SomeComponent } from './other.component'; import { SharedModule } from './shared/index' import { SomeService } from './some.service'; @NgModule({ imports: [CommonModule, SharedModule], declarations: [ SomeComponent ], exports: [ SomeComponent ] }) export class OtherModule { static forRoot(): ModuleWithProviders { return { ngModule: SharedModule, // here you put services to share providers: [SomeService] } } } // and in AppModule @NgModule({ imports: [ CommonModule, SharedModule, OtherModule.forRoot() ], declarations: [ SomeComponent ], exports: [ SomeComponent ] }) 

It will allow you to use one instance of SomeService in your components. 它允许您在组件中使用SomeService的一个实例。

I've updated your plunker. 我已经更新了你的plunker。 Pleas, have a look on changed example 请看看改变的例子

You can achieve this by using an Observable . 您可以使用Observable来实现此目的。 This observable is stored in your service and other can subscribe to this observable. 此observable存储在您的服务中,其他可以subscribe此observable。 Below is an example in which I have removed the standard Angular stuff to improve readability: 下面是一个示例,其中我删除了标准的Angular内容以提高可读性:

service.ts service.ts

@Injectable()
export class MyService {
    // BehaviorSubject sends last known value when someone subscribes. 
    public isAuthenticated$ = new BehaviorSubject<boolean>(false);

    changeAuthenticated() {
       // broadcast true to all subscribers
       this.isAuthenticated$.next(true);
    }

}

component.ts component.ts

myService.isAuthenticated$.subscribe(authenticated => {

    if (authenticated) {
        // do stuff
    }

});

So when isAuthenticated$ inside MyService is changed it fires to all subscribers. 因此,当MyService中的isAuthenticated$更改时,它会激活所有订阅者。 Please not it doesn't fire if there are no subscribers. 如果没有订阅者,请不要激活它。

More info about Observables: https://angular-2-training-book.rangle.io/handout/observables/using_observables.html 有关更多信息Observables: https//angular-2-training-book.rangle.io/handout/observables/using_observables.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM