#鸿蒙通关秘籍#如何在HarmonyOS Next中封装自定义弹窗组件并与UI解耦

HarmonyOS
7天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
星辰绘UI

在HarmonyOS Next应用中,封装一个可重用的自定义弹窗组件,与UI解耦,可以按照以下步骤进行:

  1. 封装弹窗UI组件: 创建一个弹窗选项类来封装所有的弹窗参数及样式:

    export class AppDialogOption {
      view?: WrappedBuilder<Object[]>
      buildParams?: Object
      params?: Object
      autoClose?: number
      onPop?: (data: PopInfo) => void
      onBackPressed?: () => boolean
      styles?: AppDialogStyle = new AppDialogStyle()
      animation?: TransitionEffect
      instance?: AppDialog
    }
    
    export class AppDialogStyle {
      transparent: boolean = false
      background: string = 'rgba(0,0,0,0.5)'
      radius: Length = 5
      align: Alignment = Alignment.Center
    }
    
  2. 封装弹窗控制器: 提供一套链式调用API用于管理弹窗生命周期:

    export class AppDialog {
      static indexArr: number[] = [];
      private stackIndex: number = 0;
      private options?: AppDialogOption;
    
      public static buildWithOptions(options?: AppDialogOption): AppDialog {
        let instance: AppDialog = new AppDialog();
        let index: number = AppRouter.getInstance().getPathStack().size() - 1;
        AppDialog.indexArr.push(index);
        instance.stackIndex = index;
        instance.options = options;
        options!.instance = instance;
        return instance;
      }
    
      // More methods...
    }
    
分享
微博
QQ
微信
回复
7天前
相关问题
HarmonyOS 自定义弹窗封装后不显示
326浏览 • 1回复 待解决
HarmonyOS 关于自定义弹窗封装调用
336浏览 • 2回复 待解决
如何封装全局性的自定义弹窗
280浏览 • 1回复 待解决
如何在自定义弹窗中再次弹窗
2255浏览 • 1回复 待解决