HarmonyOS Builder 函数作为一个控件的入参,Builder 函数的click this没有指向原始的this?

页面Page, 里面有一个控件View, 从Page给View 传递了一个构建的Builder函数, 但该函数的onClick this 没有指向Page。(导致title 不更新)

阅读 497
1 个回答

参考demo:

@Component
struct CustomContainer {
  @Prop header: string = '';
  @Builder defaultBulder(){}

  @BuilderParam rightBuilder: () => void = this.defaultBulder
  build() {
    Column() {

      this.rightBuilder()
    }
  }
}
@Entry
@Component
struct CustomContainerUser {
  @State text: string = 'header';

  build() {
    Column() {
      Text(this.text)
      CustomContainer({ header: this.text }) {
        Column() {
          this.rightBuilder('testA', 'testB')
        }.backgroundColor(Color.Yellow)
        .onClick(() => {
          this.text = 'changeHeader';
        })
      }
    }

  }
  @Builder  rightBuilder(label1: string, label2: string) {
    Column() {
      Button('右边按钮').onClick(() => {
        this.text = '点击了'
        console.log('qqqq1=',this)
      })
      //   }
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进