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

1 ViewChild

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