订阅了一个事件,收到通知后更新列表,但视图却无法更新,怎么办呢?
我们可以利用延时,在下一个事件循环触发更新即可。
this.freshDataSub$ = this.resourceService.freshData$.subscribe(v=>{
if(v){
this.getList();
this.ref.detectChanges();
}
});
...
async getList() {
const res: any = await this.getFiles();
if (res && res.success) {
this.treeNodes = res.data;
} else {
const timer = setTimeout(()=>{ //下一个事件循环更新视图
this.treeNodes = [];
clearTimeout(timer);
},0);
}
}
网友评论