简体   繁体   English

如何在Angular 2中在组件之间正确共享服务数据

[英]How to share service data between components correctly in Angular 2

I want to create a service to get data from .json file once and share it to multiple subscribers. 我想创建一个从.json文件中获取数据一次并将其共享给多个订阅者的服务。 But now with my solution number of requests to get data from .json file equals to a number of a subscibers for my service. 但是现在,根据我的解决方案,从.json文件获取数据的请求数量等于我的服务的子服务数量。

getconfig.service.ts getconfig.service.ts

 import {Injectable} from 'angular2/core'; import {Http, Response} from "angular2/http"; @Injectable() export class ConfigService { config: any; http: Http; constructor(http: Http) { this.http = http; console.log('Inside service'); this.config = this.http.get('/config.json'); } } 

robotui.component.ts robotui.component.ts

 ... import {ConnectionService} from '../services/connection.service'; @Component({ ... providers: [HTTP_PROVIDERS, ConfigService, ConnectionService] ... }) constructor(private _configService: ConfigService) { this._configService.config.subscribe((observer) => { console.log('Subscribe in RobotUI component', JSON.parse(observer._body)); }); } 

actual.photo.component.ts 实际照片组件

 import {Component} from 'angular2/core'; import {ConfigService} from '../services/getconfig.service'; @Component({ ... providers: [ConfigService] }) export class ActualPhotoComponent { constructor(private _configService: ConfigService) { this._configService.config.subscribe((observer) => { console.log('Subscribe in ActualPhoto component', JSON.parse(observer._body)); }); } } 

When i run it in my console i see: 当我在控制台中运行它时,我看到:

在此处输入图片说明

So, there is get request for each subscibers. 因此,每个子域都有一个get请求。 I want a solution when i get config.json file only once, save this info in a service and share it with multiple subscibers. 我只需要一次获取config.json文件,将此信息保存在服务中并与多个子服务共享时,就需要一个解决方案。

That's because 那是因为

@Component({
  ...
  providers: [ConfigService]  //<--- this creates service instance per component
})

To share data among controllers/components and to create single instance only, you have to inject your service into bootstrap function . 要在控制器/组件之间共享数据并仅创建单个实例,您必须将服务注入bootstrap function

import {ConfigService } from './path to service';

bootstrap('AppCompoent',[configService])  //<----Inject here it will create a single instance only

In subscribing component, 在订阅组件中,

robotui.component.ts robotui.component.ts

...
import {ConfigService} from '../services/getconfig.service';  //<----- Note this line here....
import {ConnectionService} from '../services/connection.service';

@Component({
  ...
  ...  // No providers needed anymore
  ...
})

constructor(private _configService: ConfigService) {
  this._configService.config.subscribe((observer) => {
    console.log('Subscribe in RobotUI component', JSON.parse(observer._body));
  });
}

actual.photo.component.ts 实际照片组件

import {Component} from 'angular2/core';
import {ConfigService} from '../services/getconfig.service';

@Component({
  ...
  ...  // No providers needed anymore...
})

export class ActualPhotoComponent {

  constructor(private _configService: ConfigService) {
    this._configService.config.subscribe((observer) => {
      console.log('Subscribe in ActualPhoto component', JSON.parse(observer._body));
    });
  }

}

This is what you should do. 这是您应该做的。

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

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