Angular:组件之间的通信@Input、@Output和ViewChild
①父组件给子组件传值
1、父组件:
ts:
export class HomeComponent implements OnInit {
public hxTitle = '我是首页的头部'; constructor() { }
ngOnInit(): void {
} run(): void {
alert('我是父组件的方法');
} } html:
<app-header [hxTitle222]="hxTitle" [run]="run" [home]='this'></app-header>
2、子组件:
ts:
import { Component, OnInit, Input } from '@angular/core'; @Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.less']
})
export class HeaderComponent implements OnInit { @Input() hxTitle222: any;
@Input() run: any;
@Input() home: any;
constructor() { } ngOnInit(): void {
}
getPRun(): void {
this.run();
this.home.run();
console.log(this.home);
} } html:
<p>{{hxTitle222}}</p>
<button (click)="getPRun()">我要执行父组件的方法</button>
②子组件给父组件传值
1、子组件:
ts:
import { Component, OnInit, Output, EventEmitter } from '@angular/core'; @Component({
selector: 'app-newschild',
templateUrl: './newschild.component.html',
styleUrls: ['./newschild.component.less']
})
export class NewschildComponent implements OnInit {
public hxMsg = '我是新闻孩子的信息';
@Output() hxMsg2 = new EventEmitter();
constructor() { } ngOnInit(): void {
} run(): void {
alert('我是新闻孩子的run方法');
} hxRun(): void {
this.hxMsg2.emit(this.run); //广播方法
}
} html:
<button (click)="hxRun()">我要触发事件,并将值传给父组件</button>
2、父组件:
ts:
import { Component, OnInit} from '@angular/core'; @Component({
selector: 'app-news',
templateUrl: './news.component.html',
styleUrls: ['./news.component.less']
})
export class NewsComponent implements OnInit { constructor() { } ngOnInit(): void {
} getRun2(e): void {
console.log(e);
}
} html:
<app-newschild (hxMsg2)="getRun2($event)"></app-newschild>
③父组件拿到子组件中的属性或方法之@ViewChild
1、子组件:ts中定义一些属性和方法
2、父组件:
ts:
import { Component, OnInit, ViewChild } from '@angular/core'; @Component({
selector: 'app-news',
templateUrl: './news.component.html',
styleUrls: ['./news.component.less']
})
export class NewsComponent implements OnInit {
@ViewChild('newschild') newschild: any; constructor() { } ngOnInit(): void {
} getMsg(): void {
alert(this.newschild.hxMsg); //子组件中定义的属性
} getRun(): void {
this.newschild.run(); //子组件中定义的方法
} } html:
<button (click)="getMsg()">获取子组件的msg</button>
<button (click)="getRun()">获取子组件的run方法</button>
<app-newschild #newschild></app-newschild>
Angular:组件之间的通信@Input、@Output和ViewChild的更多相关文章
- angular组件之间的通信
一.组件创建 直接使用 ng g component 的命令创建组件,会自动生成组件所需的文件,方便快捷. // 父组件 ng g component parent // 子组件 ng g compo ...
- Angular组件之间通讯
组件之间会有下列3种关系: 1. 父子关系 2. 兄弟关系 3. 没有直接关系 通常采用下列方式处理(某些方式是框架特有)组件间的通讯,如下: 1父子组件之间的交互(@Input/@Output/模板 ...
- ionic2+Angular 依赖注入之Subject ——使用Subject来实现组件之间的通信
在Angular+ionic2 开发过程中,我们不难发现,页面之间跳转之后返回时是不会刷新数据的. 场景一:当前页面需要登录之后才能获取数据--去登录,登录成功之后返回--页面需要手动刷新才能获取到数 ...
- angular组件间的通信(父子、不同组件的数据、方法的传递和调用)
angular组件间的通信(父子.不同组件的数据.方法的传递和调用) 一.不同组件的传值(使用服务解决) 1.创建服务组件 不同组件相互传递,使用服务组件,比较方便,简单,容易.先将公共组件写在服务的 ...
- react8 组件之间的通信
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
- vue 基础-->进阶 教程(3):组件嵌套、组件之间的通信、路由机制
前面的nodejs教程并没有停止更新,因为node项目需要用vue来实现界面部分,所以先插入一个vue教程,以免不会的同学不能很好的完成项目. 本教程,将从零开始,教给大家vue的基础.高级操作.组件 ...
- vue工程利用pubsub-js实现兄弟组件之间的通信
前言 项目是基于vue-cli创建的,不会搭建vue开发环境的同学可以百度,这里不再赘述. 步骤流程 vue项目搭建完成之后的文件图如下: 我的上一篇博客已经详细叙述vue工程中各个文件的作用,不清楚 ...
- React 学习(六) ---- 父子组件之间的通信
当有多个组件需要共享状态的时候,这就需要把状态放到这些组件共有的父组件中,相应地,这些组件就变成了子组件,从而涉及到父子组件之间的通信.父组件通过props 给子组件传递数据,子组件则是通过调用父组件 ...
- vuex-- Vue.的中心化状态管理方案(vue 组件之间的通信简化机制)
vuex-- Vue.的中心化状态管理方案(vue 组件之间的通信简化机制) 如果你在使用 vue.js , 那么我想你可能会对 vue 组件之间的通信感到崩溃 .vuex就是为了解决组件通信问题的. ...
随机推荐
- 面试常问的 25+ 个 Linux 命令
作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服务器上都是Linux系统.所以,要和服 ...
- 建议收藏,从零开始创建一个Activiti工作流,手把手教你完成
环境配置 项目环境: JDK1.8 tomcat7 maven3.5 开发工具: IDEA activiti7 创建项目 目标:创建一个maven项目,集成Activiti,并自动生成25张数据库表 ...
- 深度分析:java设计模式中的原型模式,看完就没有说不懂的
前言 原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的 ...
- CLH lock queue的原理解释及Java实现
目录 背景 原理解释 Java代码实现 定义QNode 定义Lock接口 定义CLHLock 使用场景 运行代码 代码输出 代码解释 CLHLock的加锁.释放锁过程 第一个使用CLHLock的线程自 ...
- 安装卸载nginx
http://www.nginx.cn/install ubuntu和debain下的apt方式安装软件很方便,特别是对于新手安装和卸载nginx. 由于nginx不能动态添加模块,所以会经常安装和卸 ...
- Yali 2019-8-15 test solution
T1. 送货 Description 物流公司要用m辆车派送n件货物.货物都包装成长方体,第i件的高度为hi,重量为wi.因为车很小,一辆车上的货物必须垒成一摞.又因为一些不可告人的原因,一辆车上货物 ...
- HDU 4920 Matrix multiplication 题解(内存访问连续性/卡常)
题目链接 题目大意 多组输入,给你两个n×n的矩阵,要你求他们相乘%3的值 题目思路 这个题目主要是要了解内存访问连续化,要尽量每次访问连续的内存 所以第一种方法会超时,第二种则AC.一种卡常技巧 代 ...
- [BUGCASE]FixedDataTable表格数据渲染错误
一.问题描述 广告配置中绑定第三方规格ID表格数据,有一部分展示错乱,具体如下: 表格组件使用 Facebook 的 (fixed-data-table) 组件 二.原因分析 1.检查props 先查 ...
- Alpha冲刺-第七次冲刺笔记
Alpha冲刺-冲刺笔记 这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs. ...
- od中低位地址和高位的顺序,以及数据的存放读写
在观察内存的时候应当注意"内存数据"与"数值数据"的区别. 在我们的调试环境中,内存由低到高分布,你可以简单地把这种情形理解成Win32系统在内存中由地位向高位 ...