Angular 2 组件之间如何通信?
组件之间的共享可以有好几种方式
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 组件之间如何通信?的更多相关文章
- Angular:组件之间的通信@Input、@Output和ViewChild
		
①父组件给子组件传值 1.父组件: ts: export class HomeComponent implements OnInit { public hxTitle = '我是首页的头部'; con ...
 - ionic2+Angular 依赖注入之Subject ——使用Subject来实现组件之间的通信
		
在Angular+ionic2 开发过程中,我们不难发现,页面之间跳转之后返回时是不会刷新数据的. 场景一:当前页面需要登录之后才能获取数据--去登录,登录成功之后返回--页面需要手动刷新才能获取到数 ...
 - 使用reflux进行react组件之间的通信
		
前言 组件之间为什么要通信?因为有依赖. 那么,作为React组件,怎么通信? React官网说, 进行 父-子 通信,可以直接pass props. 进行 子-父 通信,往父组件传给子组件的函数注入 ...
 - react native 之子组件和父组件之间的通信
		
react native开发中,为了封装性经常需要自定义组件,这样就会出现父组件和子组件,那么怎么在父组件和子组件之间相互通信呢,也就是怎么在各自界面push和pop.传值. 父组件传递给子组件: 父 ...
 - js组件之间的通信
		
应用场景: 1.在刷微博的时候,滑到某个头像上,会出现一张usercard(用户名片), 微博列表区有这个usercard, 推荐列表也有这个usercard,评论区也有. 2.在网上购物时,购物车安 ...
 - react8 组件之间的通信
		
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
 - Intent实现Activity组件之间的通信
		
今天讲解的是使用Intent实现Activity组件之间的通信. 一. 使用Intent显式启动Activity,Activity1àActivity2 1. ...
 - 使用Broadcast实现android组件之间的通信                                                    分类:            android             学习笔记             2015-07-09 14:16    110人阅读    评论(0)    收藏
		
android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...
 - vue 基础-->进阶 教程(3):组件嵌套、组件之间的通信、路由机制
		
前面的nodejs教程并没有停止更新,因为node项目需要用vue来实现界面部分,所以先插入一个vue教程,以免不会的同学不能很好的完成项目. 本教程,将从零开始,教给大家vue的基础.高级操作.组件 ...
 
随机推荐
- 线段树 (区间合并)【p2894】[USACO08FEB]酒店Hotel
			
Descripion 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 < ...
 - Column 1 of table 'xxx' cannot be converted from type 'varchar(33)' to type 'varchar(11)'
			
mysql主从同步失败.错误日志如下. Column 1 of table 'xxx' cannot be converted from type 'varchar(33)' to type 'var ...
 - 带WHERE子句的DELETE语句
			
由于前面我们执行“DELETE FROM T_Person”语句将数据表T_Person中的数据全部 删除了,为了演示带WHERE 子句的DELETE 语句,我们需要重新插入一些数据到T_Person ...
 - [CF468D]Tree
			
[CF468D]Tree 题目大意:  一棵\(n(n\le10^5)\)个编号为\(1\sim n\)的点的带边权的树,求一个排列\(p_{1\sim n}\),使\(\sum dis(i,p_i ...
 - 静态NAT地址转换
			
1.配置路由器端口ip(两个端口需要设置两个网段) Router(config)#inter f0/1 Router(confiog-if)#ip add 202.1.1.2 255.255.255. ...
 - easyui中一键清空搜索栏搜索条件的思路
			
$.fn.clearAllSearchPanel = function () { var $id = $(this); $id.find(".form-control").each ...
 - Metesploit使用随笔
			
平时在工作中真正用到metesploit机会不多,偶尔也会用来做漏洞验证,但是每次使用的时候都需要花点时间回忆一下具体是怎么用的,因此索性记下来方便自己,以使用Nessus扫描YS的某个硬件设备发现的 ...
 - 【JVM】调优笔记2-----JVM在JDK1.8以后的新特性以及VisualVM的安装使用
			
一.JVM在新版本的改进更新以及相关知识 1.JVM在新版本的改进更新 图中可以看到运行时常量池是放在方法区的 1.1对比: JDK 1.7 及以往的 JDK 版本中,Java 类信息.常量池.静态变 ...
 - 使用Rabbitmq.client反序列化包含Mongo.Bson.ObjectId属性实体类时抛异常
			
原因分析: 队列中存储的objectId属性是字符串,反序列化字符串转换成objectid类型时报错 解决方法: 1.定义ObjectIdConverter属性类,反序列 ...
 - WebForm页面使用Ajax
			
AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML.指一种创建交互式网页应用的网页开发技术.AJAX并非缩写词,而是由Jesse ...