<app-header #header></app-header>

<div #myBox>

   我是一个dom节点
</div> <button (click)="getChildRun()">获取子组件的方法</button>
/*

ViewChild获取dom节点
1、模板中给dom起一个名字
<div #myBox>
我是一个dom节点
</div>
2、在业务逻辑里面引入ViewChild
import { Component, OnInit,ViewChild} from '@angular/core';
3、 写在类里面 @ViewChild('myBox') myBox:any;
4、ngAfterViewInit生命周期函数里面获取dom
this.myBox.nativeElement
*/ import { Component, OnInit,ViewChild} from '@angular/core'; @Component({
selector: 'app-news',
templateUrl: './news.component.html',
styleUrls: ['./news.component.scss']
})
export class NewsComponent implements OnInit {
//获取dom节点
@ViewChild('myBox') myBox:any;
//获取一个组件
@ViewChild('header') header:any;
constructor() { } ngOnInit() {
} ngAfterViewInit(): void {
console.log(this.myBox.nativeElement);
this.myBox.nativeElement.style.width='100px';
this.myBox.nativeElement.style.height='100px';
this.myBox.nativeElement.style.background='red';
console.log(this.myBox.nativeElement.innerHTML);
} getChildRun(){ //调用子组件里面的方法
this.header.run(); }
}

效果:

最新文章

  1. 《Entity Framework 6 Recipes》中文翻译系列 (25) ------ 第五章 加载实体和导航属性之加载完整的对象图和派生类型上的导航属性
  2. mongodb的一些小总结
  3. 无线客户端框架设计(5):调用MobileAPI的设计(iOS篇)
  4. 01scala环境搭建和基础
  5. 在右键添加Cmder here选项,添加启动Cmder的快捷键
  6. JavaWeb解释一下什么是 servlet?
  7. servlet过滤器简化版
  8. Spring Boot 2.0 教程 - 深入SpringAplication
  9. [Swift]LeetCode221. 最大正方形 | Maximal Square
  10. sqlserver 脚本生成数据库文档
  11. 批量将PowerDesigner中表字段由小写变成大写
  12. Spark记录-阿里巴巴开源工具DataX数据同步工具使用
  13. CentOS 6.5下快速搭建ftp服务器[转]
  14. 【重要通知】本人所有技术文章转移至https://zzqcn.github.io
  15. PyQt5教程——介绍(1)
  16. APP界面框架初窥---标签导航
  17. rabbitmq 简单应用
  18. nginx学习与配置-安装与启动关闭管理
  19. CC2541广播机制和代码分析(未完成)
  20. 【题解】51nod 1203JZPLCM问题

热门文章

  1. xlsxwriter写入Excel文件
  2. nginx的应用【静态代理、动静分离】
  3. 使用Dell iDRAC服务器远程控制安装操作系统简要图解
  4. Python中的时间
  5. G1垃圾收集器官方文档透彻解读【官方解读】
  6. [转]神奇的 SQL 之层级 → 为什么 GROUP BY 之后不能直接引用原表中的列
  7. 【转载】浅谈HTTPS以及Fiddler抓取HTTPS协议
  8. Apache查看连接数和限制当前的连接数
  9. DOM(innerHTML和className)
  10. sping boot 笔记