CustomDialogController仅在作为@CustomDialog和@Component struct成员变量,且在@Component struct内部定义时赋值才有效。因此不可通过传递DialogController给子组件来关闭弹窗,可通过子组件调用父组件中关闭弹窗方法达到在子组件中关闭父组件弹窗的目的。
示例代码如下:
import { BusinessError } from '@kit.BasicServicesKit'; import { PromptAction } from '@kit.ArkUI'; import { DetailDialog } from './DetailPage'; @Entry @ComponentV2 struct Index { @Local message: string = "hello"; private ctx: UIContext = this.getUIContext(); private promptAction: PromptAction = this.ctx.getPromptAction(); private customDialogComponentId: number = 0; @Builder customDialogComponent() { Column() { DetailDialog({ click: () => { console.info('关闭弹窗'); this.promptAction.closeCustomDialog(this.customDialogComponentId); } }); }.height(200).padding(5).justifyContent(FlexAlign.SpaceBetween); } build() { Row() { Column({ space: 10 }) { Button('打开弹窗') .fontSize(20) .onClick(() => { this.promptAction.openCustomDialog({ builder: () => { this.customDialogComponent(); }, }) .then((dialogId: number) => { this.customDialogComponentId = dialogId; }) .catch((error: BusinessError) => { console.error(`openCustomDialog error code is ${error.code}, message is ${error.message}`); }); }); } .width('100%') .height('100%'); } .height('100%'); } }
@ComponentV2 export struct DetailDialog { @Param @Require click: () => void; build() { Column({ space: 50 }) { Blank() Text('这是弹窗内容') .fontSize(30); Row({ space: 50 }) { Button("关闭弹窗") .onClick(() => { // 相关业务逻辑 // 调用父组件关闭弹窗方法 this.click(); }); }; }; } }