HarmonyOS 一个class传递属性崩溃

一个class传递属性,使用link,报崩溃,该怎么操作 才不会报错?

HarmonyOS
20h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

@Link can decorate only member variables of custom components。使用方式参考如下:

@Component
struct Child {
  @Link name: string | undefined
  build() {
    Column() {
      Button('Child change name to Bob')
        .onClick(() => {
          this.name = "xxxx"
        })
      Button('Child change animal to undefined')
        .onClick(() => {
          this.name = undefined
        })
    }.width('100%')
  }
}
@Entry
@Component
struct Index {
  @State name: string | undefined = "xxxx"
  build() {
    Column() {
      Text(`The name is  ${this.name}`).fontSize(30)
      Child({ name: this.name })
      Button('Parents change name to xxx')
        .onClick(() => {
          this.name = "xxx"
        })
      Button('Parents change name to undefined')
        .onClick(() => {
          this.name = undefined
        })
    }
  }
}
分享
微博
QQ
微信
回复
18h前
相关问题
HarmonyOS 如何在class中启动一个线程
40浏览 • 1回复 待解决