HarmonyOS 针对tabBar的切换事件埋点监听

https://developer.huawei.com/consumer/cn/doc/best-practices-V5/bpta-application-track-practice-V5

在这个运维埋点统计最佳实践中,在tabBar做切换时,可以检测到willClick的回调,在外部无法拿到具体的组件做id和customPoperty的绑定,导致无法获取到具体的自定义信息,该如何处理?

HarmonyOS
2025-01-09 16:15:44
605浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以尝试将tab和视图分开构造布局,在自定义tabBar上绑定id和customPoperty,参见以下demo:

import { DataResource } from '../common/DataResource';

@Entry
@Component
struct Index {
  private tabsController: TabsController = new TabsController()
  @State currentIndex: number = 0;

  onPageShow(): void {
    const uiContext: UIContext = this.getUIContext();
    console.log('text-1', JSON.stringify(uiContext.getFrameNodeById('text-1')?.getCustomProperty('text-1')));
  }

  @Builder
  TabBarBuilder(title: string, targetIndex: number) {
    Text(title)
      .fontWeight(targetIndex === this.currentIndex ? FontWeight.Bold : FontWeight.Normal)
      .padding(10)
      .id('text-' + (targetIndex + 1))//设置id
      .customProperty('text-' + (targetIndex + 1), DataResource['Index']['text-'+(targetIndex + 1)])//设置customProperty
      .onClick(() => {
        this.tabsController.changeIndex(targetIndex)
      })
  }

  build() {
    Row() {
      Column() {
        Flex({ direction: FlexDirection.Row }) {
          this.TabBarBuilder('页签1', 0)
          this.TabBarBuilder('页签2', 1)
        }

        Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) {
          TabContent() {
            Text("页签1页面")
          }

          TabContent() {
            Text("页签2页面")
          }
        }
        .barHeight(0)
        .scrollable(false)
        .onChange((index: number) => {
          this.currentIndex = index;
        })
      }
    }
  }
}
  • 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.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
分享
微博
QQ
微信
回复
2025-01-09 19:52:40


相关问题
HarmonyOS(全)如何实现
616浏览 • 1回复 待解决
HarmonyOS方案
685浏览 • 1回复 待解决
HarmonyOS方案
916浏览 • 1回复 待解决
HarmonyOS怎么添加
606浏览 • 1回复 待解决
HarmonyOS 方案如何实现
764浏览 • 1回复 待解决
HarmonyOS 关于页面方案咨询
1057浏览 • 1回复 待解决
HarmonyOS Next方案如何实现
577浏览 • 1回复 待解决
分析无法正常上报
2406浏览 • 1回复 待解决
HarmonyOS应用前后台状态切换事件监听
1628浏览 • 1回复 待解决
基于UI Observer实现UI组件
1241浏览 • 1回复 待解决
在应用内采集数据方式
1320浏览 • 1回复 待解决
HarmonyOS TabBar热区事件处理
659浏览 • 1回复 待解决
HarmonyOS 怎么实现网络切换监听
663浏览 • 1回复 待解决