1. ngAfterViewInit(){
  2. //推荐在此生命周期函数中操作DOM
  3. }

1.ViewChild

  1. //1.给DOM命名
  2. <div #app *ngIf="flag">
  3. 显示
  4. </div>
  5. //2.配置ViewChild
  6. import { Component, OnInit,ViewChild } from '@angular/core';
  7. export class AboutComponent implements OnInit {
  8. @ViewChild('app',{static:false}) app:any;
  9. ngAfterViewInit(){
  10. console.log(this.app);
  11. }
  12. }