HarmonyOS自定义弹出对话框CustomDialog并传递变量

HarmonyOS定义了一系列弹窗反馈类的组件​

和前端开发框架VUE3配套生态库element plus中的提供各种组件相比,还是要少一些。可能是手机端操作和PC端操作的差异导致的​

如果内置的弹窗不满足要求,可以基于CustomDialog自定义出各种个性化的反馈组件。

首先新建一个ets文件,使用CustomDialog装饰器定义一个自定义的dialog。其内部的语法和普通的Component是完全相同的,通过组合基础组件可以实现不同的布局。

注意需要传递的参数使用@Link进行修饰,在"实例化"的时候引用

javascript 复制代码
@CustomDialog
export default struct CustomActionSheet {
  @Link actionList: string[]
  controller: CustomDialogController
  cancel: () => void


  build() {
    Column() {
      ForEach(this.actionList, (item: string) => {
        Text(item)
          .width('100%')
          .height(24)
          .fontSize(16)
          .margin({ top: 2, bottom: 2 })
          .textAlign(TextAlign.Center)
          .onClick((e)=>{

          })
      }, item => item)


      Button('取消', { type: ButtonType.Normal }).borderRadius(4).width(80).onClick((e) => {
        this.controller.close()
        this.cancel()
      })
    }
    .justifyContent(FlexAlign.SpaceAround)
    .alignItems(HorizontalAlign.Center)
    .height(200)
    .padding({ left: 10, top: 20 })
    .borderRadius(8)
  }
}

​然后在父组件中实例化CustomDialogController,并在builder属性中引用这个CustomDialog​

javascript 复制代码
diaglogController: CustomDialogController = new CustomDialogController({
    builder: CustomActionSheet({
      cancel: this.onCancel,
      actionList: $actionList
    }),
    cancel: this.existApp,
    autoCancel: true,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false
  })

在需要触发的地方调用即可​

javascript 复制代码
Button().onClick((e) => {
          if (this.diaglogController != undefined) {
            this.diaglogController.open()

          }
        })

最终效果:

​​​​​​​

相关推荐
AI_零食34 分钟前
番茄钟鸿蒙PC Electron框架完成:状态机、定时器管理与专注力工具设计
前端·javascript·华为·electron·开源·鸿蒙·鸿蒙系统
提子拌饭13334 分钟前
逛三园游戏——基于鸿蒙PC Electron框架实现
前端·javascript·游戏·华为·electron·鸿蒙
李二。36 分钟前
鸿蒙原生ArkTS-鸿蒙6.0新特性-动态模糊视频背景登录页
华为·音视频·harmonyos
李二。3 小时前
鸿蒙原生ArkTS-鸿蒙6.0新特性-粒子特效壁纸动态桌面
华为·harmonyos
科技与数码4 小时前
鸿蒙智能待办:钉钉学而思待办自动同步日历
华为·钉钉·harmonyos
不爱学英文的码字机器6 小时前
[鸿蒙PC命令行移植适配]移植rust三方库tokei到鸿蒙PC的完整实践
华为·rust·harmonyos
以太浮标6 小时前
华为eNSP模拟器综合实验之- 路由黑洞场景解析及实验
运维·网络·网络协议·网络安全·华为·智能路由器·信息与通信
EterNity_TiMe_6 小时前
[鸿蒙PC命令行移植适配]移植rust三方库ouch到鸿蒙PC的完整实践
华为·rust·harmonyos
提子拌饭1336 小时前
路径动画用鸿蒙PC用Electron框架实现流动效果技术详解
华为·electron·开源·harmonyos·鸿蒙
jingyu飞鸟6 小时前
openEuler 22.03 LTS SP4安装华为opengauss 22.03 LTS版本数据库,一键复制安装使用,保姆级教程
数据库·华为