HarmonyOS 编译报错If a component attribute supports local initialization, a valid, runtime-independent default value should be set for it
@CustomDialog
struct CustomDialogExample {
  cancel?: () => void
  confirm?: () => void
  controller: CustomDialogController
  build() {
    Column() {
      Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            this.controller.close()
            if (this.cancel) {
              this.cancel()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            this.controller.close()
            if (this.confirm) {
              this.confirm()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
  }
}
@Entry
@Component
struct CustomDialogUser {
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() },
    }),
  })
  onCancel() {
    console.info('Callback when the first button is clicked')
  }
  onAccept() {
    console.info('Callback when the second button is clicked')
  }
  build() {
    Column() {
      Button('click me')
        .onClick(() => {
          this.dialogController.open()
        })
    }.width('100%').margin({ top: 5 })
  }
}
preview会报错。
        HarmonyOS
      
        赞
        
 收藏 0
 回答 1
 
        待解决
        
相关问题
 tomcat setting should be set in tomcat preference page 
3294浏览  • 1回复 待解决
HarmonyOS 下载报错Download File Path Valid 
2542浏览  • 1回复 待解决
forEach循环中,编译报错:Not all code paths return a value 
1616浏览  • 1回复 待解决
Context类型错误,报错401“The context must be a valid Context” 
3908浏览  • 1回复 待解决
编辑XML文件时报错:Unknown HarmonyOS XML attribute怎么回事? 
8411浏览  • 1回复 待解决
HarmonyOS This line should be unreachable 
906浏览  • 1回复 待解决
HarmonyOS 编译flutter报错 
1612浏览  • 1回复 待解决
Local Emulator 如何安装apk? 
4936浏览  • 1回复 待解决
HarmonyOS 请求报错如下 {"code":2300026,"message":"Failed to open/read local data from file/application"} 
1110浏览  • 1回复 待解决
HarmonyOS NDK 运行编译报错 
1875浏览  • 1回复 待解决
export和export default的区别 
3828浏览  • 1回复 待解决
HarmonyOS native audio 录制编译报错 
1500浏览  • 1回复 待解决
HarmonyOS 使用request.uploadFile 提示401,filePath is not valid 
1336浏览  • 1回复 待解决
报错 @Prop (class SynchedPropertyOneWayPU) ‘model’[1151] <@Component ‘DTListNormalItem’[1150]>: 
2688浏览  • 1回复 待解决
build apps 时 停留在 > hvigor DEBUG default Check process is in registry... 也不报错,也没有任何的信 
1326浏览  • 1回复 待解决
编译报错Found exception如下 
3318浏览  • 1回复 待解决
HarmonyOS ets替换ts编译报错问题 
1371浏览  • 1回复 待解决
HarmonyOS  单元测试Local Test 问题 
1776浏览  • 1回复 待解决
HarmonyOS /data/local/tmp路径uri获取 
1223浏览  • 1回复 待解决
HarmonyOS 交叉编译报错,执行./build.sh GMLibDecoder指令报错 
1155浏览  • 1回复 待解决
编译报错没有堆栈信息 
1842浏览  • 1回复 待解决





















预览器不支持使用自定义弹窗,请使用模拟器测试。支持使用预览器的API清单:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-previewer-api-list-0000001741342488-V5