无法触发页面重绘, 但是可以获取到变更的值,页面无变化。

  endowDailDataValue(result: dailyDataReturnValue) {
    this.refreshingProgress = result.progress;
    if (result.progress == 100) {
      let message = result.result as dailyDataResult[];
      message.reverse()
      if (message.length != 0) {
        let data: dailDataDecodeType = healthDataDecode.messageDecode(message);
        let list = healthIndexFeature.healthItemListDecode(data, this.itemList);
        this.itemList = list;
        console.log("this.itemList=>",this.itemList)
      }
      this.isRefreshing = false;
    }
  }

调用函数,将修改的值赋值给回this.itemList,但UI并没有进行重绘,按照官方文档@State的解释,当@State修饰的数据被修改时,所在组件的build()方法会被重新调用,怀疑是数组中对象值修改,@State没有监听到的原因,需要对数组进行改动,如数组的内置方法,可将数组变为一个新的数组,我这里用的是解构;

  endowDailDataValue(result: dailyDataReturnValue) {
    this.refreshingProgress = result.progress;
    if (result.progress == 100) {
      let message = result.result as dailyDataResult[];
      if (message.length != 0) {
        let data: dailDataDecodeType = healthDataDecode.messageDecode(message);
        let list = healthIndexFeature.healthItemListDecode(data, this.itemList);
        this.itemList = [...list];
      }
      this.isRefreshing = false;
    }
  }

当然,数组中的push,pop,unshift,shift,splice,slice等都是可用对数组进行修改,使用后能被@State监听到。

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐