鸿蒙UI数据绑定不适用prop注解方式以及指定获取某个View

​鸿蒙UI数据绑定不适用prop注解方式以及指定获取某个View并操作view的方法示例

HarmonyOS
2024-06-03 23:23:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
放大电路

组件交互可以参考如下三种方式:

import HashMap from '@ohos.util.HashMap'; 
 
import EventBus from 'eventbusjs'; 
 
const KEY_: string = 'ChildComponent2'; 
const MSG_CLICK = 'msg_onclick'; 
 
@Entry 
@Component 
@Preview 
struct ParenComponent { 
  @State clickPropCount: number = 0; 
  private callbackController: DemoController = new DemoController(); 
 
  build() { 
    Row() { 
      Column() { 
        Button('方式一:通过Prop交互') 
          .width('100%').height('45vp').onClick(event => { 
          this.clickPropCount++; 
        }) 
        ChildComponent1({ clickCount: this.clickPropCount, title: '通过Prop传值' }); 
 
        Button('方式二:通过注册回调交互') 
          .width('100%').height('45vp').onClick(event => { 
          this.callbackController.listenerList.forEach((listener, key) => { 
            listener?.onChange(); 
          }) 
        }) 
        ChildComponent2({ controller: this.callbackController, title: '通过注册回调传值' }); 
 
        // https://gitee.com/openharmony-tpc/openharmony_tpc_samples/tree/master/EventBus 
        Button('方式三:通过EvtBus交互') 
          .width('100%').height('45vp').onClick(event => { 
          EventBus.dispatch(MSG_CLICK); 
        }) 
        ChildComponent3({ title: '通过EvtBus传值' }); 
      }.height('100%') 
    }.height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
分享
微博
QQ
微信
回复
2024-06-04 22:27:33
相关问题
HarmonyOS装饰器或注解实现方式咨询
1151浏览 • 1回复 待解决
HarmonyOS 设备性能数据获取方式
976浏览 • 1回复 待解决
HarmonyOS web-view的使用以及demo
889浏览 • 1回复 待解决
如何拉起拨号界面以及指定号码?
1914浏览 • 2回复 待解决
Scrapy 获取不到拉勾网指定的xpath数据
6177浏览 • 0回复 待解决
鸿蒙如何获取与设备绑定的设备ID?
1834浏览 • 1回复 待解决
HarmonyOS 获取view在屏幕的坐标
780浏览 • 1回复 待解决