HarmonyOS 进行二级请求后变量监听失效

在view中的viewmodel里进行请求后得到数据对viewmodle中变量刷新时view不会刷新。

示例场景:

class RequestData{  
  data1?:string = undefined  
  data2?:RequestData2 = undefined  
}  
class RequestData2{  
  data3?:string =undefined  
  data4?:RequestData3  
}  
class RequestData3{  
  data5:Array<string> = ["111", "222", "3333"]  
}  
class Request{  
  callback:(requestData:RequestData)=>void  
  constructor(requestData:(requestData:RequestData)=>void) {  
    this.callback = requestData  
  }  
  request(){  
    console.log("refresh1")  
    if (this.callback!==undefined){  
      console.log("refresh2")  
      let requestData:RequestData = JSON.parse('{"data1":"data1", "data2":{"data3":"data3", "data4":{"data5":["data1","data5"]}}}')  
      let requestData2:RequestData = new RequestData()  
      requestData2.data1 = "normal data1"  
      requestData2.data2 = new RequestData2()  
      requestData2.data2.data3 = "normal data3"  
      requestData2.data2.data4 = new RequestData3()  
      this.callback(requestData2)  
    }  
  }  
}  
class ViewModel{  
  data?:RequestData = undefined  
  request?:Request = new Request((requestData)=>{  
    this.data = requestData  
    console.log("refresh3")  
    console.log(JSON.stringify(this.data))  
  })  
  refreshData(){  
    this.request?.request()  
  }  
}  
@Entry  
@Component  
struct textRequestView{  
  @State @Watch("viewModelChange") viewModel:ViewModel = new ViewModel()  
  viewModelChange(){  
    console.log("refresh4")  
    console.log(JSON.stringify(this.viewModel.data))  
  }  
  build() {  
    Column(){  
      Text(this.viewModel.data?.data1 ?? "2333").padding(10).fontColor(Color.Black)  
      Text(this.viewModel.data?.data2?.data3 ?? "3222").padding(10).fontColor(Color.Black)  
      Text(this.viewModel.data?.data2?.data4?.data5.toString() ?? "1234").padding(10).fontColor(Color.Black)  
      Text(JSON.stringify(this.viewModel.data)).padding(10).fontColor(Color.Black)  
      Text("点击更新数据").onClick(()=>{  
        this.viewModel.refreshData()  
      }).padding(10).backgroundColor(Color.Pink)  
    }  
  }  
}
HarmonyOS
2024-10-16 11:56:04
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

callback 方式修改对象会丢失ViewModel的UI同步渲染效果,建议refreshData方法直接调用request对象的request方法,方法直接返回requestData2对象。

class Request {  
  request() {  
    console.log("refresh1")  
    console.log("refresh2")  
    let requestData: RequestData =  
      JSON.parse('{"data1":"data1", "data2":{"data3":"data3", "data4":{"data5":["data1","data5"]}}}')  
    let requestData2: RequestData = new RequestData()  
    requestData2.data1 = "normal data1"  
    requestData2.data2 = new RequestData2()  
    requestData2.data2.data3 = "normal data3"  
    requestData2.data2.data4 = new RequestData3()  
    return requestData2  
  }  
}  
class ViewModel {  
  data?: RequestData = undefined  
  request?: Request = new Request()  
  refreshData() {  
    this.data = this.request?.request()  
  }  
}
分享
微博
QQ
微信
回复
2024-10-16 15:47:09
相关问题
HarmonyOS 二级页面左滑关闭问题
222浏览 • 1回复 待解决
Navigation二级导航嵌套
1175浏览 • 1回复 待解决
HarmonyOS 需要二级联动的demo
275浏览 • 1回复 待解决
HarmonyOS 点击tabs如何跳转到二级页面
388浏览 • 1回复 待解决
二级浮层的出场动画实现
732浏览 • 1回复 待解决
HarmonyOS升级版本音量监听失效
511浏览 • 0回复 待解决
网络请求如何进行回调?
1005浏览 • 1回复 待解决
HarmonyOS网络请求JSON转换问题
699浏览 • 1回复 待解决