wepy ——$apply

1、说明

在异步函数中更新数据的时候,必须手动调用 $apply 方法。

 

2、代码和效果

// html
<button type="primary" plain="true">{{fruit}}</button>

// js
data = {
  fruit: '苹果'
}
onShow(){
  setTimeout(() => {
    this.fruit = "橘子";
    console.log(this.fruit);   // 橘子
    this.$apply();
  },1500)
}

 

 

如果不加 this.$apply(); 这一句,即使 fruit 已改变,但是依赖 fruit 这个数据所渲染的视图将不会更新。

posted @ 2019-11-14 17:56  徐林俊  阅读(388)  评论(0编辑  收藏  举报