Skip to content

小小前端

明月本无心,行人自回首。

Menu
  • 前端开发
  • 编程技术
  • SQL语句
  • Linux
  • 生活/旅行
  • JSEditor
  • MiniBarCMS
  • About
  • 隐私政策
Menu

[转]Angular2中组件通信

Posted on 2017年4月15日2017年4月15日 by king2088

angular2组件间是可以相互传递参数的,可以通过子组件传递到父组件,也可以由父组件传递到子组件,也可以由子组件传递到父组件,然后再传递到其他子组件。

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

父->子 input 方式


import {Component,Input} from 'angular2/core';
@Component({
selector: 'child',
template: `

child {{content}}

`
})
class Child {
@Input() content:string;
}

@Component({
selector: 'App',
directives: [Child],
template: `

App


`
})
export class App {

i:number = 0;

constructor() {
setInterval(()=> {
this.i++;
}, 1000)
}

}

子->父 output 方式


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

@Component({
selector: 'child',
template: `

child

`
})
class Child {
@Output() updateNumberI:EventEmitter = new EventEmitter();
i:number = 0;

constructor() {
setInterval(()=> {
this.updateNumberI.emit(++this.i);
}, 1000)
}
}

@Component({
selector: 'App',
directives: [Child],
template: `

App {{i}}


`
})
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: `

child

`
})
class Child {

constructor(@Host() @Inject(forwardRef(()=> App)) app:App) {
setInterval(()=> {
app.i++;
}, 1000);
}
}

@Component({
selector: 'App',
directives: [Child],
template: `

App {{i}}


`
})
export class App {
i:number = 0;
}

父获得子实例

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

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

@Component({
selector: 'child',
template: `

child {{i}}

`
})
class Child {
i:number = 0;
}

@Component({
selector: 'App',
directives: [Child],
template: `

App {{i}}


`
})
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: `

child {{service.i}}

`
})
class Child {

constructor(public service:KittencupService){

}
}

@Component({
selector: 'App',
directives: [Child],
providers: [KittencupService],
template: `

App {{i}}


`
})
export class App {

constructor(service:KittencupService) {
setInterval(()=> {
service.i++;
}, 1000)
}

}

service EventEmitter方式


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

@Injectable()
class KittencupService {
change: EventEmitter;

constructor(){
this.change = new EventEmitter();
}
}

@Component({
selector: 'child',
template: `

child {{i}}

`
})
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: `

App {{i}}


`
})
export class App {

i:number = 0;

constructor(service:KittencupService) {
setInterval(()=> {
service.change.emit(++this.i);
}, 1000)
}

}

转自:https://github.com/kittencup/angular2-ama-cn/issues/23

发表评论 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

分类

近期文章

  • cordova-plugin-camera在某些android机型中拍照或选择文件时闪退出错的解决办法 2019年10月24日
  • JavaScript nodeJS base64加密解密url参数 2019年10月15日
  • 利用expressJS编写reset api 2019年4月13日
  • angular4 + http拦截器 2019年3月21日
  • ionic navCtrl.pop如何传递参数给上一个页面 2018年11月16日
  • ionic3搭建开发/测试环境 2018年10月25日
  • ionic2、3双击硬件back按键退出应用 2018年10月24日
  • VMware安装Mac OS High Sierra 10.12及高版本无法全屏 2018年8月24日

近期评论

  • 手表资讯发表在《ReactJS环境搭建》
  • king2088发表在《ionic中使用热更新插件cordova-hot-code-push》
  • 重阳节的诗句发表在《常用的sql语句》
  • 新郎致辞发表在《PHP代码实现WordPress相关文章的几种方法》
  • 霸道总裁发表在《vsftpd 提示 unrecognized service 解决办法》

归档

标签

Ajax Android Angular APP Cordova CSS css3 express html5 ionic Java javascript jQuery Linux loading mac Mac OS mongodb MySQL node nodejs PHP react SQL SSH VirtualBox vue vue-cli win10 WordPress WP REST API 主题 兼容性 前端 备份 插件 数据库 数组 服务器 正则表达式 浏览器 热更新 目录 组件 错误
2023年 3月
一 二 三 四 五 六 日
 12345
6789101112
13141516171819
20212223242526
2728293031  
« 10月    
© 2023 小小前端 | Powered by Superbs Personal Blog theme