angular强制刷新视图的方法

有时候数据更新了视图却没更新,这个时候使用NgZone就好了,类似于React的this.setState(),和Vue的this.$set()
import { Component, OnInit } from '@angular/core';
import {NgZone} from '@angular/core'
@Component({
  selector: 'app-showmain',
  templateUrl: './showmain.component.html',
  styleUrls: ['./showmain.component.css']
})
export class ShowmainComponent implements OnInit {
 
  constructor(private zone: NgZone) { 
    this.zone.run(() => {
      // 要更新视图的代码
    })
  }
 
  ngOnInit() {
  }
 
}

猜你喜欢

转载自blog.csdn.net/weixin_45679977/article/details/104888813