angular组件之间的通信
一、组件创建
直接使用 ng g component 的命令创建组件,会自动生成组件所需的文件,方便快捷。
// 父组件
ng g component parent
// 子组件
ng g component parent/child
二、了解@Input和@Output()
@Input:将父作用域中的值“输入”到子作用域中,之后子作用域进行相关处理
@Output:子作用域触发事件执行响应函数,而响应函数方法体则位于父作用域中,相当于将事件“输出”到父作用域中,在父作用域中处理。Output一般都是一个EventEmitter的实例,使用实例的emit方法将参数emit到父组件中,触发父组件中对应的事件。
三、父组件向子组件传值(@input())
使用 @Input() 让子组件知道哪个是输入的属性,对应vue中的props。
父组件:
//parent.component.html
<div class="parent-style">
<h1>我是父组件</h1>
<app-child [sendMsg]="msg"></app-child> //sendMsg任意命名
</div> //parent.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
public msg:string = '我是父组件传递过来的数据'; //需要传递的数据
constructor() { }
ngOnInit() {
}
}
子组件:
//child.component.html
<div class="child-style">
<h3>我是子组件</h3>
<p>{{sendMsg}}</p> //查看页面数据是否显示?
</div>
//child.component.ts
import { Component, OnInit ,Input} from '@angular/core'; //引入Input
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Input() sendMsg:string; //告诉组件,sendMsg是父组件传进来的数据
constructor() { }
ngOnInit() {
}
}
四、子组件向父组件传值(@Output())
子组件:
//child.component.html
<div class="child-style">
<button type="button" (click)="send()">点击触发</button>
</div>
//child.component.ts
import { Component, OnInit ,Output,EventEmitter} from '@angular/core';//引入Output @Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Output() private outer = new EventEmitter(); //输出属性,需要定义成事件
public info = "子组件消息";
constructor() { } ngOnInit() {
}
send(){
this.outer.emit(this.info);//通过emit将信息发射出去
}
}
父组件:
//parent.component.html
<div class="parent-style">
<app-child (outer)="getData($event)"></app-child>//事件绑定获取数据
<p>{{msg}}</p>
</div>
//parent.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
msg:string;
constructor() { } ngOnInit() {
}
getData(data){
this.msg = data;
}
}
angular组件之间的通信的更多相关文章
- 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的基础.高级操作.组件 ...
- 使用Broadcast实现android组件之间的通信
android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...
随机推荐
- HSE_VALUE 修改问题
MDK的例程给的外部晶振是25Mhz,以24Mhz为例,介绍修改方法. ·1. 修改HSE_VALUE 在 “stm32f4xx.h" 搜索 ”HSE_VALUE" 会看到下面这句 ...
- 《DSP using MATLAB》Problem 8.42
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- SElinux(转)
转自:http://www.361way.com/rh134-selinux/4653.html RH134小结(四)初识SElinux 2015年8月2日admin发表评论阅读评论 一.SEli ...
- iOS开发系列-Category
Category Category是OC中特有的语法.Category的作用 * 可以在不修改原来类的基础上,为这个类扩充一些方法 * 一个庞大的类可以分为多个模块开发 * 一个庞大的类可以由多个人来 ...
- arm-linux-copydump 的使用
生成可以执行的 2 进制代码 [arm@localhost gcc]#armlinuxcopydump O binary hello hello.bin
- 启动 AXD 配置开发板
1. 启动 AXD 先启动 DragonICE Server 程序. 按如下步聚启动 AXD: 开始>所有程序>ARM Developer Suite v1.2>AXD De ...
- C 删除字符串中某个指定的字符
#include <stdio.h> char *del_char(char *str, char ch) { unsigned char i=0,j=0; while(str[i] != ...
- redis随记
CONFIG REWRITE 将config文件 将服务器当前所使用的配置记录到 redis.conf 文件中.
- Android开发 View_自定义圆环进度条View
前言 一个实现,空心圆环的自定义View,已经封装完好,可以直接使用. 效果图 代码 import android.content.Context; import android.graphics.C ...
- php 图片旋转和png透明
因需要先处理生成的二维码图片旋转,再和另外一张png图片合并,图片都是png <?php // this file writes the image into the http response ...