记录一下今天犯的一个错误@State 装饰器的属性值不能是函数类型(function),因此在定义对话框时无法将事件处理函数直接赋值给这些属性。 

报错误如下:
Error message:@Component '@Component 'PermissionDialog'[130]': Illegal variable value error with decorated variable undefined 'onPositiveClick': failed validation: 'undefined, null, number, boolean, string, or Object but not function, not V3 @observed / @track class, attempt to assign value type: 'function', value: 'undefined'!

@CustomDialog
 export default  struct PermissionDialog{
  @State titleText:string='标题';
  @State contentText:string='内容';
  @State positiveText:string='确定';
  @State negativeText:string='取消';

  // @State  onPositiveClick: () => void = () => {};
  // @State onNegativeClick: () => void = () => {}; 修改前

  private  onPositiveClick: () => void = () => {};
  private onNegativeClick: () => void = () => {}; 修改后

  controller: CustomDialogController
  build() {
    Column(){
      Text(this.titleText).margin({top:10})
      Text(this.contentText).margin({top:20,bottom:10})
      Text(this.positiveText).margin({top:20,bottom:10}).onClick(()=>this.onPositiveClick())
      Text(this.negativeText).margin({top:20,bottom:10}).onClick(()=>this.onNegativeClick())

    }.justifyContent(FlexAlign.Start).padding({left:20,right:20})

  }
}


Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐