HarmonyOS @ObjectLink到底怎么用

这个页面是数据是一个ClassA数组记作classA[],ClassA里面又有一个ClassB数组,大概是下面这样的:

@Observed
export class ClassA {
  a: number;
  datas: ClassB[];
}

@Observed
export class ClassB {
  b: number;
  c: number;
}

@Entry
@Component
struct Index {
  @State pageDatas: ClassA[] = []

  build() {


    Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {


      ForEach(this.pageDatas, (page_item: ClassA, page_index: number) => {


        TabContent() {
          PageView({
            page_item: page_item,
          })
        }

      }
    }

    @Component

    export struct PageView {
      @ObjectLink page_item: ClassA;

      build() {

        Column() {

          Text(this.page_item.a + "")
            .onClick((e: ClickEvent) => {

              弹出Dialog,
              选择a值
              this.page_item.a = selecta
            }

          ForEach(this.page_item.datas, (item: ClassB, index: number) => {

            ItemView({
              page_item: this.page_item
              item: item
            })

          }, (item: ClassB) => JSON.stringify(item))

        }

      }
    }

    @
    Component

    export struct ItemView {
      @ObjectLink page_item: ClassA;
      @ObjectLink item: ClassB;

      build() {
        Row() {


          Button("加1")
            .onClick((e: ClickEvent) => {
              this.item.b = this.item.b + 1
            }

          TextInput({ text: this.item.b === 0 ? '' : this.item.b.toString() })
            .onChange((value: string) => {
              if (value === null || value === undefined || value === '') {
                this.item.b = 0
              } else {
                this.item.b = parseInt(value);
              }
            }


          Text(this.page_item.a * item.b * item.c + "m³")
        }
      }
    }
  • 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.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.

想要的效果是:

1、在Index页面的每个page里面点击对应弹出Dialog,选择a值,能够改变Text(this. page_item.a* item .b* item.c+“m³”)显示的值

2、在TextInput中输入对应的值也能够改变Text(this. page_item.a* item .b* item.c+“m³”)显示的值

3、Button点击后也能够改变TextInput中显示的值,并且也能够改变Text(this. page_item.a* item .b* item.c+“m³”)显示的值

HarmonyOS
2024-12-18 15:53:52
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

只需要对ClassA、ClassB属性改变,从而改变UI刷新,关于使用ObjectLink装饰器 可以参考对象数组的使用,而且 发给的这个demo,无法运行,问题很多,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#对象数组\对于 这类需要深层嵌套的查看下关于复杂嵌套对象属性更改失效的案例,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#复杂嵌套对象属性更改失效

分享
微博
QQ
微信
回复
2024-12-18 18:20:26


相关问题
HarmonyOS APP开发到底哪种语言?
3047浏览 • 1回复 待解决
HarmonyOS ObjectLink使用
733浏览 • 1回复 待解决
HarmonyOS ObjectLink问题
744浏览 • 1回复 待解决
enableNativeEmbedMode怎么
2242浏览 • 1回复 待解决
HarmonyOS V2 @ObjectLink?
1026浏览 • 0回复 待解决
HarmonyOS 网络监听api怎么
682浏览 • 1回复 待解决
HarmonyOS Navigation和router怎么
962浏览 • 1回复 待解决
HarmonyOS使用@ObjectLink 数据不刷新
1402浏览 • 2回复 待解决
Observe和ObjectLink 使用
902浏览 • 1回复 待解决
HarmonyOS 左滑到底自动删除
478浏览 • 1回复 待解决
HarmonyOS 怎么c++画界面
1227浏览 • 1回复 待解决
HarmonyOS @ObjectLink和@State的相关疑问
950浏览 • 1回复 待解决
HarmonyOS 状态管理之ObjectLink的疑问
973浏览 • 1回复 待解决
鸿蒙的databinding怎么
7907浏览 • 1回复 已解决
HarmonyOS 启动首帧图片怎么全屏
1197浏览 • 1回复 待解决
HarmonyOS @objectLink+@observe父组件不更新
467浏览 • 1回复 待解决
@ObjectLink @State能同时使用吗?
167浏览 • 1回复 待解决