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 })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.

preview会报错。

HarmonyOS
2024-12-25 08:59:08
1299浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

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

分享
微博
QQ
微信
回复
2024-12-25 12:56:44


相关问题
HarmonyOS 下载报错Download File Path Valid
1973浏览 • 1回复 待解决
HarmonyOS This line should be unreachable
610浏览 • 1回复 待解决
HarmonyOS 编译flutter报错
1040浏览 • 1回复 待解决
HarmonyOS NDK 运行编译报错
1277浏览 • 1回复 待解决
Local Emulator 如何安装apk?
4658浏览 • 1回复 待解决
export和export default的区别
3273浏览 • 1回复 待解决
HarmonyOS native audio 录制编译报错
1245浏览 • 1回复 待解决
openharmony 交叉编译openjdk报错
1919浏览 • 1回复 待解决
编译报错没有堆栈信息
1623浏览 • 1回复 待解决
编译报错Found exception如下
2799浏览 • 1回复 待解决
HarmonyOS 开启编译字节码Har报错
1029浏览 • 1回复 待解决
HarmonyOS ets替换ts编译报错问题
1083浏览 • 1回复 待解决