一、组件创建

直接使用 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组件之间的通信的更多相关文章

  1. ionic2+Angular 依赖注入之Subject ——使用Subject来实现组件之间的通信

    在Angular+ionic2 开发过程中,我们不难发现,页面之间跳转之后返回时是不会刷新数据的. 场景一:当前页面需要登录之后才能获取数据--去登录,登录成功之后返回--页面需要手动刷新才能获取到数 ...

  2. 使用reflux进行react组件之间的通信

    前言 组件之间为什么要通信?因为有依赖. 那么,作为React组件,怎么通信? React官网说, 进行 父-子 通信,可以直接pass props. 进行 子-父 通信,往父组件传给子组件的函数注入 ...

  3. react native 之子组件和父组件之间的通信

    react native开发中,为了封装性经常需要自定义组件,这样就会出现父组件和子组件,那么怎么在父组件和子组件之间相互通信呢,也就是怎么在各自界面push和pop.传值. 父组件传递给子组件: 父 ...

  4. js组件之间的通信

    应用场景: 1.在刷微博的时候,滑到某个头像上,会出现一张usercard(用户名片), 微博列表区有这个usercard, 推荐列表也有这个usercard,评论区也有. 2.在网上购物时,购物车安 ...

  5. react8 组件之间的通信

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  6. Intent实现Activity组件之间的通信

    今天讲解的是使用Intent实现Activity组件之间的通信. 一.         使用Intent显式启动Activity,Activity1àActivity2 1.             ...

  7. 使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏

    android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...

  8. vue 基础-->进阶 教程(3):组件嵌套、组件之间的通信、路由机制

    前面的nodejs教程并没有停止更新,因为node项目需要用vue来实现界面部分,所以先插入一个vue教程,以免不会的同学不能很好的完成项目. 本教程,将从零开始,教给大家vue的基础.高级操作.组件 ...

  9. 使用Broadcast实现android组件之间的通信

    android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...

随机推荐

  1. AtCoder ABC 128D equeue

    题目链接:https://atcoder.jp/contests/abc128/tasks/abc128_d 题目大意 有一个双端队列,里面有 N 个整数,你可以进行如下4种操作: A:从队头那一个到 ...

  2. 拾遗:{rpm、yum及源码方式管理软件包}

    一.yum配置文件位置 /etc/yum.conf /etc/yum.repos.d/*.repo 二.yum常用命令 install pkgs reinstall pkgs update pkgs ...

  3. Spring Boot 文件下载

    1. 文件下载类 import javax.servlet.http.HttpServletResponse; import java.io.*; public class DownloadUtil ...

  4. python 17 函数基础(一)

    http://www.cnblogs.com/BeginMan/p/3171977.html 一.什么是函数.方法.过程 推荐阅读:http://www.cnblogs.com/snandy/arch ...

  5. Dribbble 流行的配色风格是什么?

    Dribbble 是众所周知的设计社群网站,在网站中有许多人分享设计作品,互相交流或从其他设计获取灵感.当然也有不少网站应运而生,例如 Freebbble 可免费下载 Dribbble 数千种设计素材 ...

  6. sql 特殊时间值 第一天或最后一天 无计算错误

    DECLARE @dt datetimeSET @dt=GETDATE() DECLARE @number intSET @number=3 --1.指定日期该年的第一天或最后一天--A. 年的第一天 ...

  7. K8S之WebApi部署

    转载声明 本文转自:ASP.NET Core on K8S学习初探(3)部署API到K8S 1.下载镜像 docker pull edisonsaonian/k8s-demo 因为是测试流程,直接把文 ...

  8. PokerNet-poker recognition: 扑克识别 (6)

    文章目录 准备 最终结果 未来改进 准备 机器: Titan XP 12GB, 64GB RAM, 机器非常强,可靠. 下次有机会购买RTX 2080 Ti 试试 最终结果 错误率可以达到万分之一,非 ...

  9. scala中Tuple简单使用

    /** * Tuple简单使用记录 * 最大22个参数 */ object TupleUse { def main(args: Array[String]): Unit = { // 简单Tuple ...

  10. python 随便笔记

    1 判断字符串中是否有数字 i.isdigit()==True else False #判断是否是数字i.isalpha()==True else False #判断是否是字母 i.isspace() ...