HarmonyOS List里面item永远不会回调onAppear和onDeatach吗?

如题:HarmonyOS List里面item永远不会回调onAppear和onDeatach吗?

阅读 455
1 个回答

经测试如下写法这两个回调可以触发:

@State listNumber: Array<number> = [0, 1, 2, 3]

List() {
  ForEach(this.listNumber, (item: number, index: number) => {
    ListItem() {
      Text(item.toString())
        .onAppear(() => {
          console.log(`${item} text appear`)
        })
        .onDetach(() => {
          console.log(`${item} text Detach`)
        })
    }
    .onAppear(() => {
      console.log(`${item} ListItem appear`)
    })
    .onDetach(() => {
      console.log(`${item} ListItem Detach`)
    })
  })
}

Button("listNumber")
  .onClick(() => {
    if (this.listNumber.length == 4) {
      this.listNumber = []
    } else {
      this.listNumber = [0, 1, 2, 3]
    }
  })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进