HarmonyOS 接口请求数据后,@State声明的对象不会更新组件

接口请求回来的数据,在LiveModel中赋值但是并不会更新组件LiveModel代码如下:

export class LiveModel {
  private static liveModel: LiveModel
  public data: LiveInfoBean = new LiveInfoBean()

  static getInstance(): LiveModel {

    if (LiveModel.liveModel === undefined) {
      LiveModel.liveModel = new LiveModel()

    }
    return LiveModel.liveModel
  }

  getLiveDetail(liveId: string) {
    VideoAgent.getLiveInfo(liveId, (data: LiveInfoObject) => {
      this.data = data.LiveTelecastDetail
    }, (error: string) => {
    })
  }
}

component代码如下:

@Component
export struct LiveComponent {
  @State model: LiveModel = LiveModel.getInstance()
  @State data: LiveInfoBean = this.model.data
  @State liveUrl: string = this.model.getLiveUrl()

  aboutToAppear(): void {
    let params: CommonParams = this.param as CommonParams
    if (this.param) {
      this.liveId = params.value
      this.model.getLiveDetail(this.liveId)
    }
  }

  build() {
    NavDestination() {

      Image(this.data.img)
        .objectFit(ImageFit.Auto)
        .width("100%")
        .height(210)
        .id("img")
        .margin({
          top: this.realTopHeight
        })
    }
  }
}

LiveInfoBean代码如下:

@Observed
export class LiveInfoBean {
  public img: string = ""
}

网络请求数据解析代码如下:

export class ProjectResultHttpListener<T> implements IHttpListener {
  private onSuccessFunc: (t: T) => void;
  private onFailureFunc: (err: string) => void;

  constructor(onSuccessFunc: (t: T) => void, onFailureFunc: (err: string) => void) {
    this.onSuccessFunc = onSuccessFunc;
    this.onFailureFunc = onFailureFunc;
  }

  onSuccess(stringResult: string): void {
    try {
      let resultObj: ProjectResultBean<T> = JSON.parse(stringResult);
      if (resultObj.errno == 0 || resultObj.error == 0) {
        this.onSuccessFunc(resultObj.data as T);
      } else if ("string" == typeof resultObj.data) {
        this.onFailureFunc(resultObj.data as string);
      } else {
        this.onFailureFunc(resultObj.msg);
      }
    } catch (error) {
      this.onFailureFunc(`error:${error}`);
    }
  }

  onFailure(err: string) {
    this.onFailureFunc(err);
  }
}
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

1、查看以下LiveTelecastDetail的路径是否正确,建议修改为:

// LiveModel.ets
getLiveDetail(liveId: string) {
  this.data = data.data.LiveTelecastDetail;
  return this.data;
}

2、在aboutToappear中赋值,这样可以再创建自定义组件的新实例后,在执行其build()函数之前获取到数据:

aboutToAppear(): void {
  let params: CommonParams = this.param as CommonParams
  if (this.param) {
    this.liveId = params.value
    this.data = this.model.getLiveDetail(this.liveId)
  }
}

3、建议使用@ObservedV2装饰器和@Trace装饰器类属性变化观测,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-new-observedv2-and-trace-V5

分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 接口请求数据处理
18浏览 • 1回复 待解决
网络请求数据处理
577浏览 • 1回复 待解决
HarmonyOS axios接口请求
146浏览 • 1回复 待解决
HarmonyOS 接口请求处理
335浏览 • 1回复 待解决
HarmonyOS LazyForEach不会更新@State
229浏览 • 1回复 待解决
HarmonyOS 接口请求处理处理
187浏览 • 1回复 待解决
HarmonyOS 网络接口请求查看功能
20浏览 • 1回复 待解决
鸿蒙JS开发 接口请求loading??
6603浏览 • 1回复 已解决
鸿蒙JS开发 接口请求loading?
5792浏览 • 1回复 待解决
HarmonyOS 如何发起网络接口请求
96浏览 • 1回复 待解决
鸿蒙JS开发 蓠接口请求loading?
3908浏览 • 1回复 待解决
HarmonyOS 提出来组件数据不会更新
502浏览 • 1回复 待解决
网络请求数据处理问题
501浏览 • 1回复 待解决
HarmonyOS @State无法更新组件文本
133浏览 • 1回复 待解决