HarmonyOS(56) 获取自定义组件的唯一ID:getUniqueId()方法

getUniqueId是API 12提供的接口,用来获取当前Component的UniqueId。UniqueId为系统为每个组件分配的Id,可保证当前应用中的唯一性。

测试代码如下,分别在PageOne、PageTwo、PageThree三个Component的aboutToAppear打印各自的UniqueId:

dart 复制代码
  aboutToAppear(): void {
    this.test();
  }
  test(){
    let uniqueId: number = this.getUniqueId();
    console.info('pageTwo uniqueId == ' + uniqueId);
  }

完整代码如下:

dart 复制代码
@Entry
@Component
struct PageOne {
  build() {
    Row() {
      Column({ space: 10 }) {
        PageTwo()
        PageThree()
      }
      .width('100%')
    }
    .height('100%')
    .backgroundColor(0xF1F3F5)
  }
  aboutToAppear(): void {
    this.test();
  }
  test(){
    let uniqueId: number = this.getUniqueId();
    console.info('pageOne uniqueId == ' + uniqueId);
  }
}
@Component
export struct PageTwo {
  @State message: string = 'Hello World 2';
  build() {
      Text(this.message)
        .id('PageTwo HelloWorld ')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)

  }
  aboutToAppear(): void {
    this.test();
  }
  test(){
    let uniqueId: number = this.getUniqueId();
    console.info('pageTwo uniqueId == ' + uniqueId);
  }
}

@Component
export struct PageThree {
  @State message: string = 'Hello  World 3';

  build() {
      Text(this.message)
        .id('PageTwo HelloWorld ')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)

  }
  aboutToAppear(): void {
    this.test();
  }
  test(){
    let uniqueId: number = this.getUniqueId();
    console.info('pageThree uniqueId == ' + uniqueId);
  }
}

运行后,日志输出如下所示:

dart 复制代码
10-24 10:14:56.135   46525-46525  A03D00/com.exa...simplme/JSAPP pid-46525             I     pageOne uniqueId == 3
10-24 10:14:56.136   46525-46525  A03D00/com.exa...simplme/JSAPP pid-46525             I     pageTwo uniqueId == 7
10-24 10:14:56.136   46525-46525  A03D00/com.exa...simplme/JSAPP pid-46525             I     pageThree uniqueId == 8
相关推荐
BlackWolfSky6 小时前
鸿蒙中级课程笔记3—ArkUI进阶6—ArkUI性能优化实践(长列表加载性能优化)
笔记·华为·harmonyos
大雷神6 小时前
HarmonyOS智慧农业管理应用开发教程--高高种地--第14篇:数据分析与可视化
华为·数据分析·harmonyos
zilikew6 小时前
Flutter框架跨平台鸿蒙开发——高尔夫计分器APP的开发流程
flutter·华为·harmonyos·鸿蒙
鸣弦artha7 小时前
Flutter框架跨平台鸿蒙开发——GridView基础入门
flutter·华为·harmonyos
芒鸽7 小时前
macos上Rust 命令行工具鸿蒙化适配完全攻略
macos·rust·harmonyos
御承扬7 小时前
鸿蒙原生系列之懒加载瀑布流组件
c++·harmonyos·懒加载·鸿蒙ndk ui·瀑布流布局
Easonmax7 小时前
基础入门 React Native 鸿蒙跨平台开发:实现一个红绿灯
react native·react.js·harmonyos
zilikew8 小时前
Flutter框架跨平台鸿蒙开发——丢件上报APP的开发流程
flutter·华为·harmonyos·鸿蒙
晚霞的不甘8 小时前
Flutter for OpenHarmony:迈向专业:购物APP的架构演进与未来蓝图
其他·flutter·架构·fiddler·前端框架·harmonyos
大雷神8 小时前
HarmonyOS智慧农业管理应用开发教程--高高种地--第15篇:地图导航与路线规划
华为·harmonyos