美文网首页
rxjs subject无法更新视图

rxjs subject无法更新视图

作者: 姜治宇 | 来源:发表于2023-03-13 14:53 被阅读0次

订阅了一个事件,收到通知后更新列表,但视图却无法更新,怎么办呢?
我们可以利用延时,在下一个事件循环触发更新即可。

    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);
      

    }
  }

相关文章

网友评论

      本文标题:rxjs subject无法更新视图

      本文链接:https://www.haomeiwen.com/subject/xzycrdtx.html