HarmonyOS 弹窗复用问题?

下面这个弹窗的代码我想去复用,所有把他定义在了一个class对象里,但是CustomDialogController仅在作为@CustomDialog和@Component struct的成员变量,且在@Component struct内部定义时赋值才有效,否则弹窗弹不出来,有什么方法可以复用吗 而不是在每个组件使用的时候去copy 代码

model: MaterialDialog.Model = new MaterialDialog.Model();
dialogAttribute = new DialogAttributeModel()
dialogController: CustomDialogController = new CustomDialogController({
  builder: MaterialDialog({ model: this.model, dialogAttribute: this.dialogAttribute }),
  cancel: this.existDialog,
  autoCancel: true,
  alignment: this.model.dialogAligmentType,
  customStyle: true
})

existDialog() {
  console.info('Click the callback in the blank area')
  this.dialogController.close()
}

showCustomDialog(context: common.UIAbilityContext, goSettingMessage: string) {
  this.model.title($r("app.string.useOhosLocationServices"))
  this.model.message(goSettingMessage)
  this.model.positiveButton($r('app.string.agree'), () => {
    context.startAbility({
      bundleName: 'com.huawei.hmos.settings',
      abilityName: 'com.huawei.hmos.settings.MainAbility',
      uri: 'application_info_entry',
      parameters: {
        // 应用包名
        pushParams: 'com.foundersc.harm.xf'
      }
    })
  })
  this.model.negativeButton($r('app.string.disagree'), () => {
  })
  this.model.dialogAligmentType = DialogAlignment.Center
  this.dialogController = new CustomDialogController({
    builder: MaterialDialog({ model: this.model, dialogAttribute: this.dialogAttribute }),
    cancel: this.existDialog,
    autoCancel: true,
    alignment: this.model.dialogAligmentType,
    customStyle: true
  })
  this.dialogController.open()
}
阅读 465
1 个回答

定义在class类里面,目前是不支持的。可以使用ohos.promptAction提供的弹窗组件,将弹窗内容封装成Builder单独放在ArkTS文件中。使用时import。

参考demo

import { BusinessError } from '@ohos.base';
import { ComponentContent } from "@ohos.arkui.node";
class Params {
  text: string = ""
  constructor(text: string) {
    this.text = text;
  }
}//传参
@Builder
function buildText(params: Params) {
  Row(){
    Column() {
      Text(params.text)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .margin({bottom: 36})
    }.backgroundColor('#FFF0F0F0')
  }
  .height("100%")
} //自定义组件的内容
@Entry
@Component
struct Index {
  @State message: string = "显示TOAST"
  build() {
    Row() {
      Column() {
        Button("click me")
          .onClick(() => {
            let uiContext = this.getUIContext();
            let promptAction = uiContext.getPromptAction();
            let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message));//上下文、自定义节点、传参
            try {
              promptAction.openCustomDialog(contentNode);
            } catch (error) {
              let message = (error as BusinessError).message;
              let code = (error as BusinessError).code;
              console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
            };
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}