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

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

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

参考demo:

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

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

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

@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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-09-02 17:43:48
相关问题
是否可以自定义权限文字
1392浏览 • 1回复 待解决
如何给自定义加上圆角背景
1709浏览 • 1回复 待解决
自定义的状态获取
600浏览 • 1回复 待解决
HarmonyOS自定义组件增加方法如何实现
131浏览 • 1回复 待解决
如何实现一个自定义询问
131浏览 • 1回复 待解决
js 自定义组件如何传递方法
5640浏览 • 2回复 待解决
HarmonyOS Web是否提供自定义dns的方法
167浏览 • 1回复 待解决
如何封装一个自定义Dialog对话
1861浏览 • 1回复 待解决
自定义弹窗自定义转场动画
654浏览 • 1回复 待解决