鸿蒙开发 组件之间的传值

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

}

相关推荐
俩毛豆1 天前
【毛豆工具集】【文件】【目录操作】生成沙盒目录
前端·javascript·鸿蒙
kirk_wang1 天前
Flutter flutter_sound 库在鸿蒙平台的音频录制与播放适配实践
flutter·移动开发·跨平台·arkts·鸿蒙
2502_946204311 天前
Cordova与OpenHarmony生长追踪系统
鸿蒙
梦想不只是梦与想1 天前
鸿蒙中 倒计时实现方法
华为·harmonyos·鸿蒙·setinterval·texttimer
世界美景2 天前
一种基于 ART 内存特征的 LSPosed/Xposed/分身环境 完美检测方案
android·安全·安卓·xposed
2502_946204312 天前
Cordova与OpenHarmony社区交流系统
鸿蒙
2502_946204312 天前
Cordova与OpenHarmony养护历史记录
鸿蒙
2502_946204312 天前
Cordova与OpenHarmony养护技巧分享
鸿蒙
BsonJ2 天前
Frida常用工具——参数说明
安卓
青春の帆は私が舵をとる2 天前
Cordova与OpenHarmony应用设置管理
鸿蒙