组件之间的共享可以有好几种方式

http://learnangular2.com/outputs/ 实例参考
https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child实例参考

http://learnangular2.com/viewChild/ 实例参考

父->子 input 方式

import {Component,Input} from 'angular2/core';
@Component({
selector: 'child',
template: `
<h2>child {{content}}</h2>
`
})
class Child {
@Input() content:string;
} @Component({
selector: 'App',
directives: [Child],
template: `
<h1>App</h1>
<child [content]="i"></child>
`
})
export class App { i:number = 0; constructor() {
setInterval(()=> {
this.i++;
}, 1000)
} }

子->父 output 方式

import {Output,EventEmitter,Component} from 'angular2/core';

@Component({
selector: 'child',
template: `
<h2>child</h2>
`
})
class Child {
@Output() updateNumberI:EventEmitter<number> = new EventEmitter();
i:number = 0; constructor() {
setInterval(()=> {
this.updateNumberI.emit(++this.i);
}, 1000)
}
} @Component({
selector: 'App',
directives: [Child],
template: `
<h1>App {{i}}</h1>
<child (updateNumberI)="numberIChange($event)"></child>
`
})
export class App { i:number = 0; numberIChange(i:number){
this.i = i;
} }

子获得父实例

如果不了解forwardRef用处的的可以看 #11
@Host 表示这个Injector必须是host element在这里可以理解为 parent

import {Host,Component,forwardRef} from 'angular2/core';

@Component({
selector: 'child',
template: `
<h2>child</h2>
`
})
class Child { constructor(@Host() @Inject(forwardRef(()=> App)) app:App) {
setInterval(()=> {
app.i++;
}, 1000);
}
} @Component({
selector: 'App',
directives: [Child],
template: `
<h1>App {{i}}</h1>
<child></child>
`
})
export class App {
i:number = 0;
}

父获得子实例

子元素指令在父constructor时是获取不到的,所以必须在组件的ngAfterViewInit生命周期钩子后才能获取,如果对组件生命周期不了解的话,可以参考 #56

import {ViewChild,Component} from 'angular2/core';

@Component({
selector: 'child',
template: `
<h2>child {{i}}</h2>
`
})
class Child {
i:number = 0;
} @Component({
selector: 'App',
directives: [Child],
template: `
<h1>App {{i}}</h1>
<child></child>
`
})
export class App { @ViewChild(Child) child:Child;
ngAfterViewInit() {
setInterval(()=> {
this.child.i++;
}, 1000)
} }

service 方式

import {Component,Injectable} from 'angular2/core';

@Injectable();
class KittencupService {
i:number = 0;
} @Component({
selector: 'child',
template: `
<h2>child {{service.i}}</h2>
`
})
class Child { constructor(public service:KittencupService){ }
} @Component({
selector: 'App',
directives: [Child],
providers: [KittencupService],
template: `
<h1>App {{i}}</h1>
<child></child>
`
})
export class App { constructor(service:KittencupService) {
setInterval(()=> {
service.i++;
}, 1000)
} }

service EventEmitter方式

import {Component,Injectable,EventEmitter} from 'angular2/core';

@Injectable()
class KittencupService {
change: EventEmitter<number>; constructor(){
this.change = new EventEmitter();
}
} @Component({
selector: 'child',
template: `
<h2>child {{i}}</h2>
`
})
class Child { public i:number = 0; constructor(public service:KittencupService){ service.change.subscribe((value:number)=>{
this.i = value;
})
}
} @Component({
selector: 'App',
directives: [Child],
providers: [KittencupService],
template: `
<h1>App {{i}}</h1>
<child></child>
`
})
export class App { i:number = 0; constructor(service:KittencupService) {
setInterval(()=> {
service.change.emit(++this.i);
}, 1000)
} }

Angular 2 组件之间如何通信?的更多相关文章

  1. Angular:组件之间的通信@Input、@Output和ViewChild

    ①父组件给子组件传值 1.父组件: ts: export class HomeComponent implements OnInit { public hxTitle = '我是首页的头部'; con ...

  2. ionic2+Angular 依赖注入之Subject ——使用Subject来实现组件之间的通信

    在Angular+ionic2 开发过程中,我们不难发现,页面之间跳转之后返回时是不会刷新数据的. 场景一:当前页面需要登录之后才能获取数据--去登录,登录成功之后返回--页面需要手动刷新才能获取到数 ...

  3. 使用reflux进行react组件之间的通信

    前言 组件之间为什么要通信?因为有依赖. 那么,作为React组件,怎么通信? React官网说, 进行 父-子 通信,可以直接pass props. 进行 子-父 通信,往父组件传给子组件的函数注入 ...

  4. react native 之子组件和父组件之间的通信

    react native开发中,为了封装性经常需要自定义组件,这样就会出现父组件和子组件,那么怎么在父组件和子组件之间相互通信呢,也就是怎么在各自界面push和pop.传值. 父组件传递给子组件: 父 ...

  5. js组件之间的通信

    应用场景: 1.在刷微博的时候,滑到某个头像上,会出现一张usercard(用户名片), 微博列表区有这个usercard, 推荐列表也有这个usercard,评论区也有. 2.在网上购物时,购物车安 ...

  6. react8 组件之间的通信

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  7. Intent实现Activity组件之间的通信

    今天讲解的是使用Intent实现Activity组件之间的通信. 一.         使用Intent显式启动Activity,Activity1àActivity2 1.             ...

  8. 使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏

    android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...

  9. vue 基础-->进阶 教程(3):组件嵌套、组件之间的通信、路由机制

    前面的nodejs教程并没有停止更新,因为node项目需要用vue来实现界面部分,所以先插入一个vue教程,以免不会的同学不能很好的完成项目. 本教程,将从零开始,教给大家vue的基础.高级操作.组件 ...

随机推荐

  1. DNS无响应

    无语,运行某某大品牌的杀毒软件后,无法上网,window检查后是DNS服务器无响应. 开始>运行>输入"netsh winsock reset"  然后重启电脑. pi ...

  2. 【莫队算法】bzoj3289 Mato的文件管理

    莫队算法,离线回答询问,按一定大小(sqrt(n*log(n))左右)将答案分块,按 ①左端点所在块②右端点 双关键字排序. 然后暴力转移. 转移的时候用树状数组. O(n*sqrt(n)*log(n ...

  3. JDBC 操作数据库

    jdbc: package org.java.dao; import java.sql.Connection; import java.sql.DriverManager; import java.s ...

  4. laravel中的事件处理

    一.什么是事件处理 事件就是在特地时间.特定地点.发生的特定行为.例如:删除某个用户帖子这个行为后,要通过站短发送信息给帖子所属的用户.这里就有删除帖子事件,发站短是事件后处理. 二.为什么要使用事件 ...

  5. Java高级架构师(一)第30节:把应用部署到Linux服务器上

  6. Scala零基础教学【41-60】

    第41讲:List继承体系实现内幕和方法操作源码揭秘 def main(args: Array[String]) { /** * List继承体系实现内幕和方法操作源码揭秘 * * List本身是一个 ...

  7. freedom isn't free

    财务自由(除去房和车) 第一阶段: 个人存款达到50万以上 第二阶段 个人存款100~200万 第三阶段 个人存款400万以上 第三阶段以上才能算实现了相对较好的财务自由!come on , boys ...

  8. RegexHelper

    ylbtech-Unitity-cs: RegexHelper 验证帮助类 1.A,效果图返回顶部   1.B,源代码返回顶部 1.B.1,RegexMail #region RegexMail pu ...

  9. Solr学习、安装与Quick Start

    之前用Lucene进行了一些简单的例子,现在安装Solr学习一下. 在mac下,貌似可以直接brew install solr来进行安装.尝试一下. 貌似安装成功了: ==> Summary

  10. 阿里云RDS(云数据库)之产品简介

    参考阿里产品文档:https://docs.aliyun.com/?spm=5176.100054.3.1.ywnrMX#/pub/rds/product-introduce/overview& ...