Services are used to share data between components. They follow a module pattern that allows you to use the data throughout your application so that your data is consistent and in sync.

Create a service.module.ts:

import { NgModule} from '@angular/core';
import {SimpleService} from './simple.service'; @NgModule({
})
export class ServicesModule {
static forRoot(){
return {
ngModule: ServicesModule,
providers: [SimpleService]
}
}
} export {
SimpleService
}

Simple.serivce.ts:

import { Injectable } from '@angular/core';

@Injectable()
export class SimpleService { message = "Hello"; constructor() { } }

app.module.ts:

import { NgModule} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {AppComponent} from './app.component';
import {HomeModule} from './components/home/home.module';
import {WidgetsModule} from './components/widgets/widgets.module';
import {ServicesModule} from './serivces/service.module'; @NgModule({
imports:[BrowserModule, HomeModule, WidgetsModule, ServicesModule.forRoot()],
declarations:[AppComponent],
bootstrap:[AppComponent]
})
export class AppModule{}

Use the SimpleService:

import { Component, OnInit } from '@angular/core';
import {SimpleService} from '../../serivces/service.module'; @Component({
moduleId: module.id,
selector: 'home',
templateUrl: 'home.component.html'
})
export class HomeComponent implements OnInit { constructor(private simpleService: SimpleService) {
console.log("message:", simpleService.message);
} ngOnInit() { } }

[Angular 2] Share a Service Across Angular 2 Components and Modules的更多相关文章

  1. angular 关于 factory、service、provider的相关用法

    1.factory() Angular里面创建service最简单的方式是使用factory()方法. factory()让我们通过返回一个包含service方法和数据的对象来定义一个service. ...

  2. 使用Angular CDK实现一个Service弹出Toast组件

    在Angular中,官方团队在开发Material组件库的同时,顺手做了一套Component dev kit,也就是在Angular世界中大名鼎鼎的CDK,这套工具包提供了非常多的前端开发的通用功能 ...

  3. [Angular 2 Router] Configure Your First Angular 2 Route

    Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and ...

  4. angular核心原理解析1:angular自启动过程

    angularJS的源代码整体上来说是一个自执行函数,在angularJS加载完成后,就会自动执行了. angular源代码中: angular = window.angular || (window ...

  5. Angular项目构建指南 - 不再为angular构建而犹豫不决(转)

    如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余 ...

  6. 【Angular专题】——(1)Angular,孤傲的变革者

    目录 一. 漫谈Angular 二. 如果你还在使用Angularjs 三. 我计划这样学习Angular技术栈 一. 漫谈Angular Angular,来自Google的前端SPA框架,与Reac ...

  7. Angular 从入坑到挖坑 - Angular 使用入门

    一.Overview angular 入坑记录的笔记第一篇,完成开发环境的搭建,以及如何通过 angular cli 来创建第一个 angular 应用.入坑一个多星期,通过学习官方文档以及手摸手的按 ...

  8. [从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)

    前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应 ...

  9. [Angular 2] Injecting a Service

    Using Services in Angular 2 is very simple. This lesson covers how to create a simple class as a Ser ...

随机推荐

  1. Sunday算法(字符串查找、匹配)

    字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...

  2. 提供给开发者 10 款最好的 Python IDE

    Python 非常易学,强大的编程语言.Python 包括高效高级的数据结构,提供简单且高效的面向对象编程. Python 的学习过程少不了 IDE 或者代码编辑器,或者集成的开发编辑器(IDE).这 ...

  3. DouNet学习_Excel导入导出

    Excel --->列是有限的 -->数据靠在单元格右边是数字类型,左边是字符串类型 把一个数字当初字符串来显示 在前面加个 ' -->用程序操作Excel 可以使用Excel的所有 ...

  4. 2015-11-02-js

    1.对象 创建方式有两种,一时通过new 后加object构造函数,二是用字面量法, var box=new object(); var box={ name='bokeyuan'; }; 访问对象: ...

  5. seo技巧-2015/10/05

    1.每页都要有它自己的文件名,并且有它自己的上级文件夹和它自己相关关键字. 2.建议在每页上使用一个的H1标签.我也试着使用许多H2 或H3的标签在页面内辅助构成正文内容. 3. 有时花费一点钱帮助你 ...

  6. QS之vcom

    -2008 | -2002 | -93 | -87 choose VHDL 2008, 2002, 1993, or 1987 -explicit resolve ambiguous overload ...

  7. searchDisplayController 时引起的数组越界

    当  [searchDisplayController.searchResultsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNo ...

  8. geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=493 Sorting Elements of an Array by Frequ ...

  9. Google Test资料

    Google Test资料 玩转Google开源C++单元测试框架Google Test系列(gtest)(总) gtest.h file not found googletest xcode 7.0 ...

  10. C#应用Newtonsoft.Json操作json[2]-反序列化不定类型

    在读json时,有时不知道对方的数据类型是什么样的,本文用Newtonsoft,把json反序列化为List>,在某种情况下还是有用的. private static List<Dicti ...