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)  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.

状态变量如下:

@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  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

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

HarmonyOS
2024-10-28 10:07:13
975浏览
收藏 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  
        })  
      }  
    }  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
分享
微博
QQ
微信
回复
2024-10-28 15:15:11


相关问题
无法检测到hpm!求助各位大佬
8291浏览 • 2回复 已解决
HarmonyOS 是否能检测到app签名
819浏览 • 1回复 待解决
HarmonyOS 如何调试检测更新
2149浏览 • 1回复 待解决
HarmonyOS 如何检测网络状态
763浏览 • 1回复 待解决
HarmonyOS 如何检测没有网络
672浏览 • 1回复 待解决
如何检测相机的可用状态
771浏览 • 0回复 待解决
HarmonyOS没有活体检测的SDK?
942浏览 • 1回复 待解决
如何检测当前相机服务的状态
3135浏览 • 1回复 待解决
HarmonyOS 下拉刷新的时候没有更新数据
1346浏览 • 1回复 待解决