ngAfterViewInit(){
/* 推荐在此生命周期函数中操作DOM */
}
1、用生命周期
//组件/.ts
/* 视图初始化完成之后 */
ngAfterViewInit(){
/* 推荐在此生命周期函数中操作DOM */
var show:any = document.getElementById("show");
console.log(show);
}
2、用ViewChild
//.html
<div #app></div>
//.ts
import { Component, OnInit , ViewChild} from '@angular/core';
...
export class AboutComponent implements OnInit {
@ViewChild('app',{static:false}) app:any;
constructor() { }
ngOnInit() {
}
/* 视图初始化完成之后 */
ngAfterViewInit(){
console.log(this.app);
}
}