Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)
原文链接
Understanding ViewChildren, ContentChildren, and QueryList in Angular
使用场景
有时候,我们想要在父组件中访问它的子组件。在Angular中可以使用ViewChildren
ViewChild
ContentChildren
来实现对子组件的访问。
假设,我们封装了一个Alert
子组件
// alert.component.html
<h1 (click)="alert()">{{type}}</h1>
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-alert',
templateUrl: './alert.component.html',
styleUrls: ['./alert.component.scss']
})
export class AlertComponent implements OnInit {
@Input() type = 'success';
constructor() { }
ngOnInit() {
}
alert() {
console.log('alert');
}
}
然后,在HomeComponent 使用它多次
// home.component.html
<app-alert></app-alert>
<app-alert type="info"></app-alert>
<app-alert type="danger"></app-alert>
ViewChildren
使用 @ViewChildren
decorator 来获取所有的子组件。@ViewChildren
支持的参数包括 directive
、component type
和 模板变量。
// home.component.js
export class HomeComponent implements OnInit, AfterViewInit {
@ViewChildren(AlertComponent) alerts: QueryList<AlertComponent>;
ngAfterViewInit() {
console.log(this.alerts);
this.alerts.forEach(alertInstance => console.log(alertInstance));
}
}
控制台打印出了3个AlertComponent的instance 对象
当@ViewChildren
的参数是 component
或者 directive
时,会返回component
或者 directive
的实例对象。
当@ViewChildren
的参数是模板变量时,会分两种情况。如果模板变量对应的是一个component
,则返回实例对象;如果模板变量对应的是一个普通html标签,则返回本地元素的引用 ElementRef
。
// home.component.html
<div class="col" #div>
<app-alert #alert1></app-alert>
<app-alert type="info"></app-alert>
<app-alert type="danger"></app-alert>
</div>
// home.component.ts
export class HomeComponent implements OnInit, AfterViewInit {
@ViewChildren('alert1') alerts: QueryList<any>;
@ViewChildren('div') div: QueryList<any>;
ngAfterViewInit() {
console.log(this.div);
this.div.forEach(inst => console.log(inst));
console.log(this.alerts);
this.alerts.forEach(alertInstance => console.log(alertInstance));
}
}
需要注意的是:如果使用了
*ngIf
来控制子组件是否显示,只有在子组件显示的时候,才能够获取到子组件。
ViewChild
如果在父组件中只有一个子组件,使用@ViewChild
比较合适。
// home.component.ts
export class HomeComponent implements OnInit, AfterViewInit {
@ViewChild('alert1') alerts: any;
@ViewChild('div') div: any;
ngAfterViewInit() {
console.log(this.div);
console.log(this.alerts);
}
}
read 参数
ElementRef
如果不想获取子组件的实例,只想访问Dom元素,可以添加read
参数
// home.component.ts
@ViewChild('alert1', {read: ElementRef}) alerts: any;
@ViewChildren
也支持read 参数。
ViewContainerRef
You need this token when you need to create templates or components dynamically。当需要动态创建组件时,需要这个参数。
@ViewChildren(AlertComponent, {read: ViewContainerRef}) alerts: QueryList<AlertComponent>;
Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)的更多相关文章
- Vee-validate 父组件获取子组件表单校验结果
vee-validate 是为 Vue.js 量身打造的表单校验框架,允许您校验输入的内容并显示对应的错误提示信息.它内置了很多常见的校验规则,可以组合使用多种校验规则,大部分场景只需要配置就能实现开 ...
- 子组件获取父组件数据 propsDown, 父组件获取子组件数据 eventUp
(一) popsDowm 三种方法获取父组件数据:被动获得(1):主动获取(2). 1.被动获得: 父组件:v-bind: 绑定变量参数和方法参数:子组件:props 接收参数.可以在模板中直接使用也 ...
- vue父组件获取子组件页面的数组 以城市三级联动为例
父组件调用子组件 <Cselect ref="registerAddress"></Cselect> import Cselect from '../../ ...
- 关于Vue中,父组件获取子组件的数据(子组件调用父组件函数)的方法
1. 父组件调用子组件时,在调用处传给子组件一个方法 :on-update="updateData" 2. 子组件在props中,接收这个方法并声明 props: { onUp ...
- React Hook父组件获取子组件的数据/函数
我们知道在react中,常用props实现子组件数据到父组件的传递,但是父组件调用子组件的功能却不常用.文档上说ref其实不是最佳的选择,但是想着偷懒不学redux,在网上找了很多教程,要不就是hoo ...
- antd 父组件获取子组件中form表单的值
还是拿代码来讲吧,详情见注释 子组件 import React, { Component } from 'react'; import { Form, Input } from 'antd'; con ...
- vue 的父组件和子组件互相获取数据和方法
父组件主动获取子组件的数据和方法 一.ref(但不能实时更新获取) 1.调用子组件的时候 定义一个ref <child ref="headerChild"></c ...
- vue3 template refs dom的引用、组件的引用、获取子组件的值
介绍 通过 ref() 还可以引用页面上的元素或组件. DOM 的引用 <template> <div> <h3 ref="h3Ref">Tem ...
- vue:父子组件间通信,父组件调用子组件方法进行校验子组件的表单
参考: ElementUI多个子组件表单的校验管理:https://www.jianshu.com/p/541d8b18cf95 Vue 子组件调用父组件方法总结:https://juejin.im/ ...
随机推荐
- ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机【OK】
ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机[OK] 使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能 ...
- qt5--列表控件QListWidget
需要 #include <QListWidget> #include <QListWidgetItem> 列表控件可以让我们以列表形式呈现内容,是界面 ...
- centos7安装android studio遇到Unable to run mksdcard sdk tool
centos系统为最小化安装,所以安装新软件时缺少许多依赖包,Android Studio下载的mksdcard是32位的,而系统是64位的,所以需要安装支持32位软件的依赖包. sudo yum i ...
- conda退出base 环境
安装conda后取消命令行前出现的base,取消每次启动自动激活conda的基础环境 方法一: 每次在命令行通过conda deactivate退出base环境回到系统自动的环境 方法二 1,通过 ...
- python 创建实例对象
实例化类其他编程语言中一般用关键字 new,但是在 Python 中并没有这个关键字,类的实例化类似函数调用方式. 以下使用类的名称 Employee 来实例化,并通过 __init__ 方法接收参数 ...
- Arthas--Java在线分析诊断工具(阿尔萨斯)
序言 Arthas是一款阿里巴巴开源的 Java 线上诊断工具,功能非常强大,可以解决很多线上不方便解决的问题. 资料 https://blog.csdn.net/youanyyou/article/ ...
- 手动升级 Confluence - 开始升级之前
在本指南中,我们将会帮助你使用 zip / tar.gz 文件将你的 Confluence 安装实例在 Windows 或者 Linux 版本中升级到最新的版本. 升级到任何最新的版本都是免费的,如果 ...
- 2018美团CodeM编程大赛初赛B轮 A题开关灯
题目描述 美团的办公室一共有n层,每层有m个会议室,可以看成是一个n*m的网格图.工程师们每天的工作需要协作的地方很多,经常要到会议室开会解决各种问题.公司是提倡勤俭节约的,因此每次会议室只在使用时才 ...
- LOJ #6358 前夕 (组合计数、容斥原理)
题目链接 https://loj.ac/problem/6358 (另外一道\(4\)的倍数题左转loj #6356) 题意 题面写得就像一坨X一样,我来复述一下吧. 有\(N\)个元素构成的集合,要 ...
- USACO2018DEC PLATINUM
就按(博主认为的)难度顺序排吧. Sort It Out 分析 容易发现选出的集合一定是所有逆序对的一个最小覆盖集,那么剩下的就一定是一个LIS.仔细想想还可以发现字典序第\(k\)小的最小覆盖集的补 ...