鸿蒙开发 组件之间的传值

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()

}

相关推荐
<小智>8 小时前
及时做APP开发实战(十二)-LazyForEach懒加载优化实践
数据库·鸿蒙
xd18557855512 小时前
表情包配文-基于鸿蒙的AI表情包配文生成应用开发实践
人工智能·华为·harmonyos·鸿蒙
念雨思14 小时前
HarmonyOS AI 应用开发实战:短视频选题灵感 —— AI 驱动的内容创作引擎
人工智能·学习·华为·harmonyos·鸿蒙
千逐6820 小时前
HarmonyOS 实战 | 数据存本地——Preferences 轻量存储入门
华为·harmonyos·鸿蒙
<小智>21 小时前
及时做APP开发实战(十三)-已完成数量统计修复
鸿蒙
绝世番茄1 天前
鸿蒙原生 ArkTS 布局之道:Grid 替代多层嵌套布局深度解析
华为·harmonyos·鸿蒙
●VON1 天前
鸿蒙 PC Markdown 编辑器可靠性设计:未保存内容的崩溃恢复闭环
华为·中间件·编辑器·harmonyos·鸿蒙
<小智>1 天前
及时做APP开发实战(十五)-专注历史记录功能实现
鸿蒙
●VON1 天前
鸿蒙 PC Markdown 编辑器导出架构:安全 HTML 与系统 PDF
华为·架构·编辑器·harmonyos·鸿蒙
条tiao条2 天前
Navigation页面跳转教学博客
ui·华为·harmonyos·鸿蒙·页面跳转