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为例. 效果如图: 布局 ...
随机推荐
- sacnf遇到空格与回车问题
看scanf输入的是多个字符还是一个字符.如果是多个字符,遇空格和回车都不会结束输入:如果是一个字符,遇空格不会结束,遇回车结束输入.如:①#include<stdio.h> ...
- vue点击跳转拨号界面
<a :href="'tel:' + VipInfo.HotelPhone" style="text-decoration:none;color:black;opa ...
- iOS组件化开发-CocoaPods简介
CocoaPods简介 任何一门开发语言到达一定阶段就会出现第三方的类库管理工具,比如Java的Maven.WEB的Webpack等.在iOS中类库的管理工具-CocoaPods. 利用CocoaPo ...
- iOS开发系列-Charles
概述 Charles相当于一个插在服务器和客户端之间的"过滤器".当客户端向服务器发起请求的时候,先到charles进行过滤,然后charles在把最终的数据发送给服务器: 注意: ...
- Ubuntu下怎么退出vim编辑器
vim其实就是vi的升级版,vi里的所有命令vim里都可以用,一般使用来说几乎没什么差别. 写在前面:进入vim编辑器之后无法退出并保存,可以尝试给要编辑的文件sudo权限(例如:sudo vim / ...
- JAVA算法之递归
Ⅰ.三角数字 首先我们来看一组数字:1,3,6,10,15,21.....,在这个数列中第n项是由n-1项加n得到的,这个序列中的数字称为三角数字因为他们可以形象化地表示成一个三角形排列.如下图 通过 ...
- leetcood学习笔记-3-无重复字符的最长子串
题目描述: 方法一:O(N) class Solution: def lengthOfLongestSubstring(self, s: str) -> int: slow = 0 fast = ...
- 【JZOJ6288】旋转子段
description analysis 可以先用前缀和把原串不调整的方案数先求出来 对于一种翻转,肯定是把\([i..a[i]]\)或\([a[i]..i]\)这段区间翻转 也可以看做是以\({i+ ...
- DEDE采集时自动生成摘要和关键字
1.修改 include/dedecollection.class.php //自动分析关键字和摘要 preg_match("/<meta[\s]+name=['\"]key ...
- clover无缘无故隐藏书签栏原因
可能是不小心按住了Ctrl+shift+B