HarmonyOS 自定义弹框-点击按钮调用方法崩溃

登录页面点击登录按钮,弹出隐私协议确认弹框,点击弹框确认按钮,调用登录方法就会崩溃

HarmonyOS
2024-09-02 09:56:52
906浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

参考demo:

// cancel: this.onCancel, // confirm: this.onAccept,

cancel: ()=> { this.onCancel() }, 
    confirm: ()=> { this.onAccept() },
  • 1.
  • 2.

方法的传递这样就不会崩溃 上面的传递无法识别是方法体

@CustomDialog 
struct CustomDialogExample { 
  controller: CustomDialogController 
  cancel: () => void = () =>{ 
 
  } 
  confirm: () => void = () => { 
 
  } 
 
  build() { 
    Column() { 
      Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 }) 
      Flex({ justifyContent: FlexAlign.SpaceAround }) { 
        Button('cancel') 
          .onClick(() => { 
            this.controller.close() 
            this.cancel() 
          }).backgroundColor(0xffffff).fontColor(Color.Black) 
        Button('confirm') 
          .onClick(() => { 
            this.controller.close() 
            this.confirm() 
          }).backgroundColor(0xffffff).fontColor(Color.Red) 
      }.margin({ bottom: 10 }) 
    } 
  } 
} 
 
@Entry 
@Component 
struct DialogExample { 
  dialogController: CustomDialogController = new CustomDialogController({ 
    builder: CustomDialogExample({ 
      // cancel: this.onCancel, 
      // confirm: this.onAccept, 
 
      cancel: ()=> { this.onCancel() }, 
      confirm: ()=> { this.onAccept() }, 
    }), 
    alignment: DialogAlignment.Default,  // 可设置dialog的对齐方式,设定显示在底部或中间等,默认为底部显示 
  }) 
  onCancel() { 
    console.info('Callback when the first button is clicked') 
  } 
  onAccept(){ 
    console.info('Callback when the second button is clicked') 
    this.tes3t() 
  } 
  tes3t(){ 
    console.info('test when the second button is clicked') 
  } 
  build() { 
    Flex({ justifyContent: FlexAlign.Center }) { 
      Button('click me') 
        .onClick(() => { 
          this.dialogController.open() 
 
        }) 
    }.width('100%') 
  } 
}
  • 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.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
分享
微博
QQ
微信
回复
2024-09-02 17:43:48


相关问题
HarmonyOS 自定义封装问题
782浏览 • 1回复 待解决
HarmonyOS 自定义组件问题
1244浏览 • 1回复 待解决
HarmonyOS 自定义不能全屏
888浏览 • 1回复 待解决
自定义的状态获取
1566浏览 • 1回复 待解决
是否可以自定义权限文字
2515浏览 • 1回复 待解决
HarmonyOS app版本升级需要自定义
884浏览 • 1回复 待解决
HarmonyOS 如何设置自定义的颜色
676浏览 • 1回复 待解决
HarmonyOS 自定义遮罩透传问题
703浏览 • 1回复 待解决
如何给自定义加上圆角背景
2820浏览 • 1回复 待解决
自定义如何在UIAbility中弹出?
224浏览 • 0回复 待解决
自定义,遮罩背景颜色无法设置
991浏览 • 1回复 待解决
HarmonyOS 自定义关闭后页面上移
709浏览 • 1回复 待解决
HarmonyOS 背景色如何自定义图片
631浏览 • 1回复 待解决
HarmonyOS 自定义方法链式调用
859浏览 • 1回复 待解决
HarmonyOS 自定义导致机测不通过
763浏览 • 1回复 待解决