<legend id='YOOsu'><style id='YOOsu'><dir id='YOOsu'><q id='YOOsu'></q></dir></style></legend>

      <small id='YOOsu'></small><noframes id='YOOsu'>

        • <bdo id='YOOsu'></bdo><ul id='YOOsu'></ul>
      1. <i id='YOOsu'><tr id='YOOsu'><dt id='YOOsu'><q id='YOOsu'><span id='YOOsu'><b id='YOOsu'><form id='YOOsu'><ins id='YOOsu'></ins><ul id='YOOsu'></ul><sub id='YOOsu'></sub></form><legend id='YOOsu'></legend><bdo id='YOOsu'><pre id='YOOsu'><center id='YOOsu'></center></pre></bdo></b><th id='YOOsu'></th></span></q></dt></tr></i><div id='YOOsu'><tfoot id='YOOsu'></tfoot><dl id='YOOsu'><fieldset id='YOOsu'></fieldset></dl></div>
      2. <tfoot id='YOOsu'></tfoot>

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

        How to share service data between components correctly in Angular 2(如何在 Angular 2 中正确地在组件之间共享服务数据)

        <small id='pjiCt'></small><noframes id='pjiCt'>

          • <bdo id='pjiCt'></bdo><ul id='pjiCt'></ul>
          • <legend id='pjiCt'><style id='pjiCt'><dir id='pjiCt'><q id='pjiCt'></q></dir></style></legend>
              <tbody id='pjiCt'></tbody>
              <i id='pjiCt'><tr id='pjiCt'><dt id='pjiCt'><q id='pjiCt'><span id='pjiCt'><b id='pjiCt'><form id='pjiCt'><ins id='pjiCt'></ins><ul id='pjiCt'></ul><sub id='pjiCt'></sub></form><legend id='pjiCt'></legend><bdo id='pjiCt'><pre id='pjiCt'><center id='pjiCt'></center></pre></bdo></b><th id='pjiCt'></th></span></q></dt></tr></i><div id='pjiCt'><tfoot id='pjiCt'></tfoot><dl id='pjiCt'><fieldset id='pjiCt'></fieldset></dl></div>

                  <tfoot id='pjiCt'></tfoot>
                  本文介绍了如何在 Angular 2 中正确地在组件之间共享服务数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想创建一个服务来从 .json 文件中获取一次数据并将其共享给多个订阅者.但是现在我的解决方案从 .json 文件中获取数据的请求数等于我的服务的订阅者数.

                  I want to create a service to get data from .json file once and share it to multiple subscribers. But now with my solution number of requests to get data from .json file equals to a number of a subscibers for my service.

                  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

                  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:

                  因此,每个订阅者都有获取请求.当我只获得一次 config.json 文件时,我想要一个解决方案,将此信息保存在服务中并与多个订阅者共享.

                  So, there is get request for each subscibers. I want a solution when i get config.json file only once, save this info in a service and share it with multiple subscibers.

                  推荐答案

                  那是因为

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

                  <小时>

                  要在控制器/组件之间共享数据并仅创建单个实例,您必须将服务注入 bootstrap 函数.

                  import {ConfigService } from './path to service';
                  
                  bootstrap('AppCompoent',[configService])  //<----Inject here it will create a single instance only
                  

                  在订阅组件中,

                  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));
                      });
                    }
                  
                  }
                  

                  这是你应该做的.

                  这篇关于如何在 Angular 2 中正确地在组件之间共享服务数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                • <i id='lLrzz'><tr id='lLrzz'><dt id='lLrzz'><q id='lLrzz'><span id='lLrzz'><b id='lLrzz'><form id='lLrzz'><ins id='lLrzz'></ins><ul id='lLrzz'></ul><sub id='lLrzz'></sub></form><legend id='lLrzz'></legend><bdo id='lLrzz'><pre id='lLrzz'><center id='lLrzz'></center></pre></bdo></b><th id='lLrzz'></th></span></q></dt></tr></i><div id='lLrzz'><tfoot id='lLrzz'></tfoot><dl id='lLrzz'><fieldset id='lLrzz'></fieldset></dl></div>

                  <small id='lLrzz'></small><noframes id='lLrzz'>

                  • <tfoot id='lLrzz'></tfoot>
                      <bdo id='lLrzz'></bdo><ul id='lLrzz'></ul>

                            <tbody id='lLrzz'></tbody>
                            <legend id='lLrzz'><style id='lLrzz'><dir id='lLrzz'><q id='lLrzz'></q></dir></style></legend>