【发布时间】:2021-02-23 09:05:30
【问题描述】:
我有一个角度组件,它使用图像标签显示从 html 上的 websocket 获取的 base64 图像字符串。我每秒轮询一次 websocket 并尝试在页面上显示它。这是我的代码:
组件的html:
<div class="row">
<div class= "video">
<img [src]="_DomSanitizationService.bypassSecurityTrustUrl(getBase64Image())" style="margin-left:30%;width:800px;height:400px;">
</div>
</div>
打字稿代码:
import { DomSanitizer } from '@angular/platform-browser';
import { io } from 'socket.io-client';
export class HelloComponent implements OnInit{
messageText: string;
messages: Array<any>;
socket: any;
subscription: Subscription;
intervalId: any;
image: any;
constructor(public _DomSanitizationService: DomSanitizer ) {
}
ngOnInit(){
this.getLatestFrame();
this.intervalId = setInterval(this.getLatestFrame, 1000);
}
getLatestFrame(){
this.socket = io(environment.websocket_url);
this.socket.emit('hello', {
msg: 'Client to server, can you hear me server?'
});
this.socket.on('frame', (data: any) => {
this.image = data;
});
}
ngOnDestroy() {
clearInterval(this.intervalId);
}
getBase64Image(): string {
return "data:image/png;base64, " + this.image;
}
}
使用此代码,我可以看到 getLatestFrame() 每隔 1 秒就会被调用一次,就像我预期的那样。 getBase64Image() 也被调用,我可以每秒看到新的 base64 字符串,这意味着图像发生了变化,但是在 UI 上,我只能看到在 ngInit 期间加载的第一帧,之后没有任何变化。不明白为什么。
【问题讨论】:
-
试过但对我没有帮助,没有错误但图像不刷新
标签: angular