HarmonyOS @Builder方法能否像组件一样被其它页面共用,并且可以使用其调用页面的@State值

HarmonyOS
2024-12-27 15:01:16
471浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

目前@Builder使用export即可在其他页面使用。传值方法参考以下代码:

interface Pos {
  x: number;
  y: number;
}
@Builder function overBuilder($$: Pos) {
  Row() {
    Column() {
      Text(改变位置).position({x:$$.x, y:$$.y})
      HelloComponent({positionXNum: $$.x, positionYNum: $$.y})
    }
  }
}
@Component
struct HelloComponent {
  @Link positionXNum: number;
  @Link positionYNum: number;
  build() {
    Row() {
      Text(当前X= + this.positionXNum)
      Text(当前Y= + this.positionYNum)
    }
  }
}

@Entry
@Component
struct Parent {
  @State pos: Pos = { x: 0, y: 0 };
  @State posX: number = 0;
  @State posY: number = 0;
  build() {
    Column() {
      overBuilder(this.pos)
      Button(‘Click me’).onClick(() => {
        // After Click me is clicked, the UI text changes from Hello to ArkUI.
        this.posX = this.posX+100;
        this.posY = this.posY+100;
      })
    }
  }
}
  • 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.

可以在单独的文件中定义,供外部使用。

分享
微博
QQ
微信
回复
2024-12-27 16:16:20
相关问题
JS swiper 怎么list一样动态添加item?
7121浏览 • 1回复 待解决
HarmonyOS 请提供登录页面的实现
1117浏览 • 1回复 待解决