您好,我是 Angular 新手,遇到了一点问题,这是我的代码:
import { Component, OnInit,Input, SimpleChange, OnChanges } from '@angular/core';
import { Sensor } from '../../shared/sensor.model';
import { MessageService } from '../../shared/message.service';
@Component({
selector: 'app-sensor-list',
templateUrl: './sensor-list.component.html',
styleUrls: ['./sensor-list.component.css']
})
export class SensorListComponent implements OnInit {
sensors:Sensor[]=[];
sensorsId : number[]=[];
@Input() messages:JSON[]=[];
constructor(private messageService: MessageService) { }
ngOnInit() {
setInterval(()=>{
this.messages=this.messageService.getMqttdata();
console.log("updating Sensors sources")
},5000);
}
ngOnChanges(changes SimpleChange }) {
console.log("sources has been modified")
}
在 ngOnInit 中,我每 5 秒收集一次数据并将这些数据存储到我的消息变量中,这是一个 Input(),所以它应该每 5 秒触发一次 ngOnChanges 吗?我错过了什么?