加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

service – Angular 2,一次从服务器加载数据并将结果共享给组件

发布时间:2020-12-17 08:02:50 所属栏目:安全 来源:网络整理
导读:参见英文答案 What is the correct way to share the result of an Angular Http network call in RxJs 5?21个答案使用服务在Angular 2应用程序中存储(和共享)初始值的最佳实践是什么? 我有一个服务,从服务器加载大量数据作为资源,配置和其他数组和对象。
参见英文答案 > What is the correct way to share the result of an Angular Http network call in RxJs 5?21个答案使用服务在Angular 2应用程序中存储(和共享)初始值的最佳实践是什么?
我有一个服务,从服务器加载大量数据作为资源,配置和其他数组和对象。
每次加载组件或路由到视图时,我都不想加载这些数据,我只想在应用程序启动时使用这些对象和已加载的数组,并根据需要重新加载。
问题是存储此值的正确位置以及如何在使用该服务的组件之间共享?
谢谢。
您必须考虑共享服务并确保只在组件之间共享单个实例。

shared service and shared object demo

注意:不要忘记在bootstrap函数中注册服务。深入观察代码。你会得到你想要的。未演示路由部分。冲浪插入进一步实施

service.ts

import {Component,Injectable,Input,Output,EventEmitter} from 'angular2/core'
import {Router} from 'angular2/router';
import {Http} from 'angular2/http';


export interface Info {
   name:string;
}

@Injectable()
export class NameService {

  constructor(http:Http;router:Router)
  {
    this.http=http;
    // you can call server resource from here and store it down to any variable. 
  }

  info: Info = { name : "Jack" };
  change(){
    this.info.name = "Jane"; // this.info is shared among components.

  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读