HarmonyOS鸿蒙Next中嵌套自定义弹窗二级弹窗改变如何更新一级弹窗页面数据
HarmonyOS鸿蒙Next中嵌套自定义弹窗二级弹窗改变如何更新一级弹窗页面数据 嵌套自定义弹窗CustomDialogController,第一层弹窗操作点击弹出第二层弹窗,然后在第二层弹窗输入内容后关闭第二层弹窗,如何更新第一层弹窗关联的数据字段
通过@Link实现,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box-V5#示例1
参考demo如下:
// xxx.ets
@CustomDialog
struct CustomDialogExample2 {
[@Link](/user/Link) text: string
controllerTwo?: CustomDialogController
build() {
Column() {
Text('我是第二个弹窗')
.fontSize(30)
.height(100)
Button('点我关闭第二个弹窗')
.onClick(() => {
if (this.controllerTwo != undefined) {
this.text = '第二个弹窗'
this.controllerTwo.close()
}
})
.margin(20)
}
}
}
@CustomDialog
@Component
struct CustomDialogExampleOne {
@State text:string = ''
[@Link](/user/Link) textValue: string
[@Link](/user/Link) inputValue: string
dialogControllerTwo: CustomDialogController | null = new CustomDialogController({
builder: CustomDialogExample2({
text: $text,
}),
alignment: DialogAlignment.Bottom,
onWillDismiss:(dismissDialogAction: DismissDialogAction) => {
console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
console.log("dialog onWillDismiss")
if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
dismissDialogAction.dismiss()
}
if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
dismissDialogAction.dismiss()
}
},
offset: { dx: 0, dy: -25 }
})
controller?: CustomDialogController
cancel: () => void = () => {}
confirm: () => void = () => {}
build() {
Column() {
Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
TextInput({ placeholder: '', text: this.text }).height(60).width('90%')
.onChange((value: string) => {
this.text = value
})
Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel')
.onClick(() => {
if (this.controller != undefined) {
this.controller.close()
this.cancel()
}
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('confirm')
.onClick(() => {
if (this.controller != undefined) {
this.textValue = this.text
this.inputValue = this.textValue
this.controller.close()
this.confirm()
}
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.margin({ bottom: 10 })
Button('点我打开第二个弹窗')
.onClick(() => {
if (this.dialogControllerTwo != null) {
this.dialogControllerTwo.open()
}
})
.margin(20)
}.borderRadius(10)
// 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。
}
}
@Entry
@Component
struct Index28 {
@State textValue: string = ''
@State inputValue: string = 'click me'
dialogController: CustomDialogController | null = new CustomDialogController({
builder: CustomDialogExampleOne({
cancel: () => { this.onCancel() },
confirm: () => { this.onAccept() },
textValue: $textValue,
inputValue: $inputValue
}),
cancel: this.exitApp,
autoCancel: true,
onWillDismiss:(dismissDialogAction: DismissDialogAction) => {
console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
console.log("dialog onWillDismiss")
if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
dismissDialogAction.dismiss()
}
if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
dismissDialogAction.dismiss()
}
},
alignment: DialogAlignment.Bottom,
offset: { dx: 0, dy: -20 },
gridCount: 4,
customStyle: false,
cornerRadius: 10,
})
aboutToDisappear() {
this.dialogController = null // 将dialogController置空
}
onCancel() {
console.info('Callback when the first button is clicked')
}
onAccept() {
console.info('Callback when the second button is clicked')
}
exitApp() {
console.info('Click the callback in the blank area')
}
build() {
Column() {
Button(this.inputValue)
.onClick(() => {
if (this.dialogController != null) {
this.dialogController.open()
}
}).backgroundColor(0x317aff)
}.width('100%').margin({ top: 5 })
}
}
更多关于HarmonyOS鸿蒙Next中嵌套自定义弹窗二级弹窗改变如何更新一级弹窗页面数据的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,嵌套自定义弹窗时,若二级弹窗改变需要更新一级弹窗页面数据,可以通过事件订阅与发布机制实现。具体步骤如下:
-
定义事件:在二级弹窗中定义事件,用于通知一级弹窗数据变化。可以使用
EventBus或自定义事件中心。 -
发布事件:当二级弹窗中的数据发生变化时,发布相应事件。例如,使用
EventBus的post()方法或自定义事件中心的emit()方法。 -
订阅事件:在一级弹窗中订阅该事件,以便在接收到事件时更新页面数据。使用
EventBus的subscribe()方法或自定义事件中心的on()方法。 -
更新数据:在事件处理函数中,根据事件携带的数据更新一级弹窗的页面数据。可以通过调用
setState()或直接操作UI组件来更新视图。
例如:
// 二级弹窗
function SecondDialog() {
const onDataChange = (newData) => {
EventBus.post('dataChanged', newData);
};
return (
<Button onClick={() => onDataChange('new data')}>Change Data</Button>
);
}
// 一级弹窗
function FirstDialog() {
const [data, setData] = useState('initial data');
useEffect(() => {
const subscription = EventBus.subscribe('dataChanged', (newData) => {
setData(newData);
});
return () => {
EventBus.unsubscribe(subscription);
};
}, []);
return <Text>{data}</Text>;
}
通过这种方式,二级弹窗的数据变化可以实时更新到一级弹窗中。
在HarmonyOS鸿蒙Next中,嵌套自定义弹窗时,若二级弹窗需要更新一级弹窗的页面数据,可以通过以下步骤实现:
- 回调函数:在二级弹窗中定义回调函数,用于传递数据。
- 事件触发:在二级弹窗中触发事件时,调用回调函数并传递更新后的数据。
- 接收数据:在一级弹窗中接收回调函数传递的数据,并更新页面。
例如,在二级弹窗中:
onButtonClick() {
this.callback(this.updatedData);
}
在一级弹窗中:
openSecondaryDialog() {
const secondaryDialog = new SecondaryDialog();
secondaryDialog.callback = (data) => {
this.updatePrimaryData(data);
};
}
通过这种方式,二级弹窗可以更新一级弹窗的数据。

