实例如下:
import { Component,OnInit,ChangeDetectionStrategy,ChangeDetectorRef,OnDestroy} from "@angular/core";
@Component({
changeDetection:ChangeDetectionStrategy.OnPush
})
export class xxxComponent{
private timer;
constructor(private ref : ChangeDetectorRef){
this.timer = setInterval(()=>{
this.ref.detechChanges();//检测变化
},5000)
}
//销毁组件时清除定时器
ngOnDestroy(){
if(this.timer){
clearInterval(this.timer);
}
}
}