HarmonyOS 状态更新没有检测到

组件如下:

@Component  
export struct TestObservedComponent {  
  @Provide('testViewModel') testViewModel: TestViewModel = new TestViewModel()  
  
  aboutToAppear(): void {  
    this.testViewModel.currentPoint.time = 1  
  }  
  
  build() {  
    Column(){  
      TestComponent()  
    }  
  }  
}  
  
  
@Component  
struct TestComponent {  
  @State knowLedgeList: Point[] = [new Point(1),new Point(2),new Point(3)]  
  @Consume('testViewModel') testViewModel: TestViewModel  
  
  build() {  
    Column(){  
      List() {  
        ForEach(this.knowLedgeList, (model: Point, index: number) => {  
          ListItem(){  
            TestItemComponent({  
              point: model,  
              currentPoint: this.testViewModel.currentPoint,  
              selectPoint: this.selectAction  
            })  
          }  
        })  
      }  
      .width('100%')  
      .height('100%')  
      .margin({  
        top: 10,  
        left:10  
      })  
    }  
  }  
  
  selectAction = (pointModel: Point) => {  
    this.testViewModel.changePoint(pointModel)  
  }  
}  
  
@Component  
struct TestItemComponent {  
  point: Point = new Point()  
  // @Prop currentPoint: Point  
  @ObjectLink currentPoint: Point  
  selectPoint?: (pointModel: Point) => void  
  
  build() {  
    Text(`${this.point.time} - ${this.currentPoint.time}----`)  
      .width(100)  
      .height(100)  
      .onClick(this.onSelectPoint)  
  }  
  
  onSelectPoint = () => {  
    this.selectPoint?.(this.currentPoint)  
  }  
}

状态变量如下:

@Observed  
export class TestViewModel {  
  currentPoint: Point = new Point()  
  
  changePoint(pointModel: Point) {  
    this.currentPoint = pointModel  
  }  
}  
  
@Observed  
export class Point{  
  time: number = 0  
  
  constructor(time: number = 0) {  
    this.time = time  
  }  
}

点击更新状态, 没有反应。

HarmonyOS
2024-10-28 10:07:13
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

​这边建议您使用新版状态管理框架,相关文档如下

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-new-observedv2-and-trace-V5

按照您给出的代码,重新整理出demo如下:​

@ObservedV2  
class TestViewModel {  
  @Trace currentPoint: Point = new Point()  
  
  changePoint(pointModel: Point) {  
    this.currentPoint = pointModel  
    console.log("pointModel:-----" + JSON.stringify(pointModel) + "-----currentPoint:-----" +  
    JSON.stringify(this.currentPoint))  
  }  
}  
  
@ObservedV2  
class Info {  
  @Trace knowLedgeList: Point[]  
  
  constructor() {  
    this.knowLedgeList = [new Point(1), new Point(2), new Point(3)]  
  }  
}  
  
@ObservedV2  
class Point {  
  @Trace time: number = 0  
  
  constructor(time: number = 0) {  
    this.time = time  
  }  
}  
  
@Entry  
@Component  
struct TestObservedComponent {  
  testViewModel: TestViewModel = new TestViewModel()  
  info: Info = new Info()  
  
  aboutToAppear(): void {  
    this.testViewModel.currentPoint.time = 1  
  }  
  
  build() {  
    Column() {  
      Column() {  
        List() {  
          ForEach(this.info.knowLedgeList, (model: Point) => {  
            ListItem() {  
              Text(`${model.time} - ${this.testViewModel.currentPoint.time}----`)  
                .width(100)  
                .height(100)  
                .onClick(() => {  
                  this.testViewModel.changePoint(model)  
                  console.log(`${model.time} - ${this.testViewModel.currentPoint.time}----`)  
                })  
            }  
          })  
        }  
        .width('100%')  
        .height('100%')  
        .margin({  
          top: 10,  
          left: 10  
        })  
      }  
    }  
  }  
}
分享
微博
QQ
微信
回复
2024-10-28 15:15:11
相关问题
无法检测到hpm!求助各位大佬
7232浏览 • 2回复 已解决
HarmonyOS 如何调试检测更新
239浏览 • 1回复 待解决
如何检测相机的可用状态
58浏览 • 0回复 待解决
如何检测当前相机服务的状态
2073浏览 • 1回复 待解决
HarmonyOS没有活体检测的SDK?
88浏览 • 1回复 待解决
HarmonyOS 系统环境检测
88浏览 • 1回复 待解决
HarmonyOS 活体检测回调问题
75浏览 • 1回复 待解决
HarmonyOS 如何检测webview滚动是否触底
305浏览 • 1回复 待解决
HarmonyOS应用更新demo
242浏览 • 1回复 待解决