父子组件间点击事件,有没有方法能够控制点击事件在不同的情况触发父/子组件的点击事件

一个父子组件都有一个点击事件,因为子组件的层级在上,始终触发的是子组件点击事件,有没有方法能够控制点击事件在不同的情况触发父/子组件的点击事件.

HarmonyOS
2024-06-03 23:11:18
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
JKings
@Component 
export struct CountDownComponent { 
  @Prop count: number = 0; 
  costOfOneAttempt: number = 1; 
  @State countDownStartValue: number = 20; 
  @State son: HitTestMode = HitTestMode.None 
 
  build() { 
    Column() { 
      Text(this.countDownStartValue + '') 
      // @Prop装饰的变量不会同步给父组件 
      Button(`Try again`) 
    } 
    .hitTestBehavior(this.son) 
    .height(300) 
    .width(300) 
    .backgroundColor('gray') 
    .onClick(() => { 
      console.log('111111111111') 
    }) 
  } 
} 
 
@Entry 
@Component 
struct ParentComponent { 
  @State countDownStartValue: number = 10; 
 
  build() { 
    Stack() { 
      Column() { 
        CountDownComponent({ count: this.countDownStartValue, costOfOneAttempt: 2 }) 
      } 
    } 
    // .hitTestBehavior(HitTestMode.Default) 
    .zIndex(999) 
    .width(310) 
    .height(310) 
    .backgroundColor("red") 
    .onClick(() => { 
      console.log('1111111111112222222222') 
    }) 
  } 
}

文档链接:

触摸测试控制

分享
微博
QQ
微信
回复
2024-06-04 22:07:40
相关问题
组件中如何处理组件点击事件
1069浏览 • 1回复 待解决
组件事件能否到传递组件
869浏览 • 1回复 待解决
组件有没有支持事件穿透属性
487浏览 • 1回复 待解决
点击事件冒泡不符合预期
316浏览 • 1回复 待解决
服务卡片java如何设置点击事件
7834浏览 • 1回复 待解决
点击事件,@State 页面未生效,在线等
1341浏览 • 0回复 待解决
组件调用组件方法
424浏览 • 1回复 待解决