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为例. 效果如图: 布局 ...
随机推荐
- Python flask构建微信小程序订餐系统✍✍✍
Python flask构建微信小程序订餐系统 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题, ...
- 数据分析相关概念(numpy)
矢量 矢量是指一堆形成的集合. 多维数组也叫做矢量化计算. 单独一个数叫做标量 例: import datetime as dt import numpy as np n2=10000 start2 ...
- 1-MySQL高级-视图
视图 1. 问题 对于复杂的查询,往往是有多个数据表进行关联查询而得到,如果数据库因为需求等原因发生了改变,为了保证查询出来的数据与之前相同,则需要在多个地方进行修改,维护起来非常麻烦 解决办法:定义 ...
- mycat-zookeepr--mycatweb
##############################mycat镜像############################## 5-1 创mycat镜像 wget http://dl.myca ...
- 【POJ】2240 Arbitrage
题目链接:http://poj.org/problem?id=2240 题意:n种国家的货币,m个换算汇率.问你能不能赚钱. eg:1美元换0.5英镑,1英镑换10法郎,1法郎换0.21美元,这样1美 ...
- HTTP协议:响应消息
一.请求消息:客户端发送给服务器端的数据 数据格式: 1.请求行 2.请求头 3.请求空行 4.请求体 二.响应消息:服务器端发送给客户的数据 数据格式: 1.响应行: 1.组成:协议/版本 响应状态 ...
- markdown 表情包大法
前段时间偶然发现了markdown竟然可以插入表情,而且竟然如此的简单 表情包网站 (有可能是官网):点击跳转 这些东西真的是有点意思啊,容我举个栗子
- JS对象 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
返回指定的字符串首次出现的位置 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(substring, startpos) 参 ...
- 服务启动脚本start_boot.sh
vim start_boot.sh #!/bin/bash usage(){ echo "$0 [start|stop|usage]" } status_springboot(){ ...
- windows 10 无法启动 windows update 服务 错误 0x80070005 拒绝访问
windows 10 无法启动 windows update 服务 错误 0x80070005 拒绝访问: 解决方法: 首先重命名系统盘 windows目录下的代号为“SoftwareDistribu ...