鸿蒙开发 组件之间的传值

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

}

相关推荐
我命由我123454 天前
Android Cordova 开发 - Cordova 快速入门(Cordova 环境配置、Cordova 第一个应用程序)
android·开发语言·前端框架·android studio·h5·安卓·android-studio
百锦再6 天前
SQLiteDatabase 增删改查(CRUD)详细操作
android·xml·java·ide·android studio·安卓
周胡杰7 天前
鸿蒙-跨设备互通,设备互通提供跨设备的相机、扫描、图库访问能力,平板或2in1设备可以调用手机的相机、扫描、图库等功能。
数码相机·华为·自动化·电脑·harmonyos·鸿蒙·鸿蒙系统
杯莫停丶8 天前
对象池模式在uniapp鸿蒙APP中的深度应用
uni-app·harmonyos·鸿蒙
百锦再8 天前
Android Drawable 目录下的 XML 图形文件详解
android·xml·java·app·手机·安卓
百锦再8 天前
Android ImageButton 使用详解
android·java·app·安卓·studio·mobile
杯莫停丶8 天前
基于uniapp的鸿蒙APP大数据量性能优化
性能优化·uni-app·harmonyos·鸿蒙
@小代9 天前
OpenHarmony系统-源码下载,环境搭建,编译,烧录,调试
harmonyos·鸿蒙·鸿蒙系统
百锦再10 天前
Android Studio 实现自定义全局悬浮按钮
android·java·ide·app·android studio·安卓
梦想不只是梦与想11 天前
鸿蒙系统开发中路由使用详解
华为·harmonyos·鸿蒙