鸿蒙开发 组件之间的传值

1.@Prop:父组件传递给子组件,单向传递,子组件改变值,父组件UI不更新。

引入子组件 并赋值,子组件用@Prop 接收

import headerView from '../../common/bean/BaseNavHeaderView'

复制代码
headerView({
  titlestr:'添加地址',
  isback:true,
  isleftimage:'',
  isright:true,
  rightcontentstr:'保存',
  rightimage:'',
  fromehere:'添加地址',
  startCalcute:this.startCalcuteMoney.bind(this)
}).width('100%').height('44').backgroundColor('#FFD615').zIndex(888)
复制代码
@Component
export default  struct BaseNavHeaderView {

  @Prop titlestr:string;
        isback:Boolean;
        isleftimage:string;
        isright:Boolean;
        rightcontentstr:string;
       rightimage:string;
       fromehere:string;
      startCalcute:() => void = () =>{}

2.@Link 双向传递,子组件改变父组件UI更新,注意:不能修饰带@Entry 类型的pages.

父试图定义变量,传递给子组件child时要用$

复制代码
@State dianjicounts:number = 0

child({dianjicounts:$dianjicounts})

子组件接收

@Component export default struct child {

@Link dianjicounts:number

调用用this.dianjicounts

}

3.@Provide/@Consume 不需要传递参数,UI会更新

父组件定义并调用子组件

@Provide dianjicounts: number = 0

child()

子组件接收

@Component export default struct child {

@Consume dianjicounts:number

调用用this.dianjicounts

}

4.方法传递

父组件定义方法

startCalcuteMoney():void{

console.log('保存一下')

}

传给子组件,要用bind方法绑定一下

headerView({

startCalcute:this.startCalcuteMoney.bind(this)

})

子组件接收

@Component export default struct BaseNavHeaderView {

@Prop startCalcute:() => void = () =>{}

子组件调用

this.startCalcute()

}

相关推荐
dvlinker12 小时前
鸿蒙 PC 命令行工具迁移实战 · 四种命令行移植方案详解及对比
鸿蒙·移植·命令行工具
●VON13 小时前
鸿蒙NEXT ArkUI进阶:用CustomBuilder打造高定制化品牌页签栏
java·华为·harmonyos·鸿蒙·新特性
●VON17 小时前
鸿蒙NEXT实战:用HdsTabs构建沉浸式音乐播放器 WaveFlow
华为·harmonyos·鸿蒙·新特性
●VON17 小时前
鸿蒙NEXT视觉特效深度解析:gradientMask渐变遮罩与Material材质系统
华为·harmonyos·鸿蒙·材质·新特性
●VON18 小时前
BodyAR 骨骼数据解析:从 ARBody 到 23 个关键点的完整数据链路
华为·harmonyos·鸿蒙·新特性
●VON18 小时前
BodyAR 会话管理深度剖析:ARConfig 参数全解与生命周期最佳实践
华为·harmonyos·鸿蒙·新特性
●VON18 小时前
BodyAR 骨骼可视化渲染:Shape + Line + Circle 打造优雅的 AR 骨架叠加层
华为·ar·harmonyos·鸿蒙·新特性
●VON19 小时前
鸿蒙NEXT新特性:HdsTabs悬浮页签栏barFloatingStyle完全入门指南
华为·harmonyos·鸿蒙·新特性
会Tk矩阵群控的小木1 天前
rcs安卓增强短信群发系统搭建与API集成实战教程
矩阵·新媒体运营·安卓·个人开发·tk
●VON2 天前
BodyAR 从零开始:开发环境搭建与完整项目配置指南
华为·harmonyos·鸿蒙·新特性