HarmonyOS 间接使用自定义Dialog,controller属性undefined问题

//Index.ets
import { MyDialog } from './MyDialog'
import { TitleButton } from './TitleButton'
@Entry
@Component
struct MyPage {
  build() {
    Column(){
      TitleButton({
        title:'OpenMyDialog',
        customDialogController: new CustomDialogController({
          builder:MyDialog()
        })
      })
    }
  }
}
//MyDialog.ets
@Component
@CustomDialog
export struct MyDialog {
  controller: CustomDialogController;
  build() {
    Button('Quit')
      .onClick(()=>{
        this.controller?.close()
      })
  }
}
//TitleButton.ets
@Component
export struct TitleButton{
  @Prop title:string;
  customDialogController?: CustomDialogController;
  build() {
    Column(){
      Button(this.title)
        .onClick(()=>{
          this.customDialogController?.open()
        })

    }
  }
}
  • 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.

Demo代码文件上面给出,这个场景是 MyPage => TitleButton => MyDialog,我直接在MyPage中new一个CustomDialogController传给TitleButton,builder使用MyDialog,这个demo下,MyDialog弹窗中的Quit按钮是无法退出弹窗的,调试发现MyDialog中的controller为undefined。

若修改Index.ets和TitleButton.ets如下,则Quit按钮正常退出,程序正常运行,请问为什么上面那种不行。

//修改后Index.ets
import { TitleButton } from './TitleButton'
@Entry
@Component
struct MyPage {
  build() {
    Column(){
      TitleButton({
        title:'OpenMyDialog',
      })
    }
  }
}
//修改后TitleButton.ets
import { MyDialog } from './MyDialog';
@Component
export struct TitleButton{
  @Prop title:string;
  customDialogController?: CustomDialogController;
  build() {
    Column(){
      Button(this.title)
        .onClick(()=>{
          this.customDialogController=new CustomDialogController({
            builder:MyDialog()
          })
          this.customDialogController?.open()
        })
    }
  }
}
  • 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.
HarmonyOS
2024-12-25 08:14:16
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

CustomDialogController仅在作为@CustomDialog和@Component struct的成员变量,且在@Component struct内部定义时赋值才有效,请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box-V5#customdialogcontroller

分享
微博
QQ
微信
回复
2024-12-25 10:07:42


相关问题
HarmonyOS 自定义dialog相关问题
789浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
1324浏览 • 1回复 待解决
HarmonyOS 自定义Dialog高度问题
834浏览 • 1回复 待解决
HarmonyOS如何自定义组件的Controller
1270浏览 • 1回复 待解决
HarmonyOS 自定义Dialog宽度
879浏览 • 1回复 待解决
HarmonyOS 自定义全局dialog
757浏览 • 1回复 待解决
HarmonyOS 自定义全屏dialog
946浏览 • 1回复 待解决
HarmonyOS 自定义Dialog背景色透明问题
1957浏览 • 1回复 待解决
HarmonyOS 自定义dialog open无效
1005浏览 • 1回复 待解决
HarmonyOS 如何封装自定义Dialog
851浏览 • 1回复 待解决
HarmonyOS 用CustomDialog自定义Dialog
1172浏览 • 1回复 待解决