下面这个弹窗的代码我想去复用,所有把他定义在了一个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()
}
定义在class类里面,目前是不支持的。可以使用ohos.promptAction提供的弹窗组件,将弹窗内容封装成Builder单独放在ArkTS文件中。使用时import。
参考demo