Angular中使用ViewChild获取dom节点
- 模板中给dom起一个名字
<div #myBox>我是一个dom节点</div>
- 在业务逻辑里面引入ViewChild
import {Component,OnInit,ViewChild} from '@angular/core'
- 写在类里面
@ViewChild('myBox') myBox:any;
- 在ngAfterViewInit生命周期函数里面获取dom
this.myBox.nativeElement
父组件中使用ViewChild调用子组件的方法
- 模板中给子组件dom起一个名字
<app-header #header>我是一个dom节点</app-header>
- 在父组件业务逻辑里面引入ViewChild
import {Component,OnInit,ViewChild} from '@angular/core'
- 写在类里面
@ViewChild('header) header:any;
- 在ngAfterViewInit生命周期函数里面获取整个子组件实例dom
- 现在就可以在父组件中调用子组件的方法
this.header.run()
https://angular.cn/api/core/ViewChild#description
https://www.jianshu.com/p/57ed7747c45d
https://blog.youkuaiyun.com/hbiao68/article/details/86611457
https://blog.youkuaiyun.com/yusirxiaer/article/details/113839269