当前位置: 首页 > news >正文

鸿蒙:父组件调用子组件的三种方案

前言:

这篇博客会列举鸿蒙开发中父组件调用子组件的三种方案,相信你在看完以后会有所收获。

还是老样子,我们跟着官方文档学习与实践,链接如下:

https://developer.huawei.com/consumer/cn/doc/best-practices/bpta-ui-component-encapsulation#section1186112410119https://developer.huawei.com/consumer/cn/doc/best-practices/bpta-ui-component-encapsulation#section1186112410119根据官方文档提供的思路,我们便可结合现有的知识与经验动手练一练。

本人实践后整理的运行效果图和代码如下:

方案一:

利用装饰器@State 与 @Prop 的状态变量单项绑定特性
父组件的message传递给子组件,父组件修改父组件的message,子组件监听message,并执行方法。

1、Index.ets

import { Method01 } from "./Method01";@Entry
@Component
export struct Index {build() {Column() {Method01()}}
}

2、Method01.ets

// 方案一:利用装饰器@State 与 @Prop 的状态变量数据绑定特性
// 父组件的message传递给子组件,父组件修改父组件的message,子组件监听message,并执行方法
@Component
export struct Method01 {@State message: string = "这是一个父组件"@State childMessage: string = ""build() {Column({ space: 20 }) {Text("调用子组件方法示例").padding(30)Button(this.message).onClick(() => {const child = "修改"this.childMessage = child})Child({ propMessage: this.childMessage })}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}// 子组件
@Component
struct Child {@Watch("childClick") @Prop propMessage: string = ""@State message: string = "这是一个子组件"childClick() {this.getUIContext().showAlertDialog({ message: this.message })}build() {Column() {Text(this.message).onClick(() => {console.log("click")})}}
}

方案二:

通过 Controller 类实现父组件 与子组件 的通信。

子组件将自身 action 方法赋值给父组件传入的 controller 实例;父组件点击按钮时调用该 controller 的 action 方法,会触发子组件显示包含自身 message 的弹窗。

1、Index.ets

import { Method01 } from "./Method01";
import { Method02 } from "./Method02";@Entry
@Component
export struct Index {build() {Column() {Method02()}}
}

2、Method02.ets

class Controller {action = () => {};
}@Component
export struct Method02 {@State message: string = "这是一个父组件"actionController: Controller = new Controller()build() {Column({ space: 20 }) {Text("调用子组件方法示例 02").padding(30)Button(this.message).onClick(() => {this.actionController?.action()})Child({ controller: this.actionController })}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}@Component
export struct Child {@State message: string = "这是一个子组件"controller: Controller | undefined = undefined;aboutToAppear(): void {if (this.controller) {this.controller.action = this.action}}action = () => this.getUIContext().showAlertDialog({ message: this.message })build() {Column() {Text(this.message)}}
}

方案三:

使用emitter通信。

父组件 通过使用 emitter 触发指定 ID 的事件;子组件 监听该事件并触发指定方法,并在销毁时移除事件监听。

1、Index.ets

import { Method01 } from "./Method01";
import { Method02 } from "./Method02";
import { Method03 } from "./Method03";@Entry
@Component
export struct Index {build() {Column() {Method03()}}
}

2、Method03.ets

import { emitter } from "@kit.BasicServicesKit";const EMITTER_ID = "Method03";// 父组件 Method03 通过点击按钮使用 emitter 触发指定 ID 的事件;子组件 Child 监听该事件以显示弹窗,并在销毁时移除事件监听。
@Component
export struct Method03 {@State message: string = "这是一个父组件"build() {Column({ space: 20 }) {Text("调用子组件方法示例 03").padding(30)Button(this.message).onClick(() => {emitter.emit(EMITTER_ID);})Child()}.width("100%").height("100%").justifyContent(FlexAlign.Center)}
}@Component
export struct Child {@State message: string = "这是一个子组件";action = () => {this.getUIContext().showAlertDialog({ message: "这是一个子组件" })}aboutToAppear(): void {emitter.on(EMITTER_ID, this.action);}aboutToDisappear(): void {emitter.off(EMITTER_ID);}build() {Text(this.message)}
}

有帮助就给个关注、收藏或赞吧

http://www.dtcms.com/a/388874.html

相关文章:

  • AppTest邀请测试 -邀请用户
  • 从零开始的云计算生活——第六十五天,鹏程万里,虚拟化技术
  • Java 开发指南:将 PDF 转换为多种图片格式
  • 【C++革命】董翔箭头函数库(xiang_arrow):在main函数里定义函数的终极方案
  • Ubuntu显示No operation system found
  • 【深度学习新浪潮】音频大模型方面有哪些最新的研究进展?
  • 第3节 创建视频素材时间线到剪映(Coze扣子空间剪映小助手零基础教程)
  • Unifi AP 网络路由取消使用 无线 Meshing
  • 计算机网络基础(四) --- TCP/IP网络结构(网络层) (上)
  • AR巡检与区块链融合:工业智能化的新引擎
  • Product Hunt 每日热榜 | 2025-09-18
  • WPF 字符竖向排列的排版格式(直排)显示控件
  • 多色零件自动化分拣与追溯系统案例和项目落地全计划
  • 自动化面试常见问题(英文版)
  • Kettle Carte 服务实战:从作业提交到日志监控全流程自动化(附 Shell 脚本)
  • 【数字展厅】数字科技展厅是怎么建设沉浸式体验的?
  • 2025网安周|美创科技多地联动,共筑数据安全防线
  • 数字大健康:一场重塑未来的健康革命,正被科技重新定义
  • 手搓一个可以自动化对比yolo模型性能曲线的工具
  • 海图科技双撕裂检测装置:筑牢矿用皮带运输安全防线
  • 32、语言模型训练全流程:从数据到模型的工程化实现
  • 打造一款支持 Mermaid 与 ECharts 的 Markdown 编辑器:基于 Vditor 的实战指南
  • 《算法闯关指南:优选算法-双指针》--07三数之和,08四数之和
  • 华为显卡部署
  • Salesforce知识点:LWC(Lightning Web Components)面试题及答案
  • 【C/C++】一文通关C/C++内存管理:动态开辟改朝换代——new/delete
  • 安卓13_ROM修改定制化-----修改rom 实现“usb安装”选项默认开启
  • Git 常用命令速查表
  • Day45 51单片机UART串口通信与数码管时钟系统
  • 企业级图像AIGC技术观察:Seedream 4.0 模型能力与应用场景分析