HarmonyOS CustomDialog中的CustomDialogController 为undefined?

自定义对话中的持有的CustomDialogController变量为undefined

咨询场景描述:当对话框new CustomDialogController 出现在局部方法中

代码:在组件中增加方法如下,则TMsgDialog中持有的CustomDialogController为undefined

showMsgDialog(msg: string) {
  let dialogController = new CustomDialogController({
    builder: TMsgDialog({
      msg: msg,
      rightBtn: "我知道了",
      rightCallback: () => {
        dialogController.close();
      }
    }),
    customStyle: true,
    autoCancel: false,
    alignment: DialogAlignment.Center,
    maskColor: 0x77000000
  });
  dialogController.open();
}
阅读 502
1 个回答

这是因为这里在方法中写的controller是一个局部变量,dialogController的声明应该是在@Component中作为一个变量进行初始化,此外,也不能动态的对dialogController进行赋值。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进