当前位置: 首页 > article >正文

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

getUniqueId是API 12提供的接口,用来获取当前Component的UniqueId。UniqueId为系统为每个组件分配的Id,可保证当前应用中的唯一性。
测试代码如下,分别在PageOne、PageTwo、PageThree三个Component的aboutToAppear打印各自的UniqueId:

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

完整代码如下:



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);
  }
}

export struct PageTwo {
   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);
  }
}


export struct PageThree {
   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);
  }
}

运行后,日志输出如下所示:
在这里插入图片描述

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

http://www.kler.cn/news/365725.html

相关文章:

  • C++20中头文件syncstream的使用
  • Axios 基本使用
  • nginx 日志配置笔记
  • Android Audio基础——音频流重采样(十三)
  • 数据结构模板代码合集(不完整)
  • com.baomidou.mybatisplus.extension.service.IService用法详解及使用例子
  • 企业如何配合好等级保护测评工作?
  • 聚簇索引与非聚簇索引
  • 【Unity】Unity中调用手机的震动功能 包括安卓和IOS
  • 鸿蒙开发融云demo发送文本消息
  • fpga系列 HDL: 竞争和冒险 01
  • JMeter与大模型融合应用之JMeter创建二级菜单的简单大模型交互
  • 企业自建邮件系统选U-Mail ,功能强大、安全稳定
  • jenkins国内插件源
  • 深入解析 MySQL 数据库:更新和删除
  • 【Java小白图文教程】-05-数组和排序算法详解
  • docker 可用镜像服务地址(2024.10.25亲测可用)
  • 【ChatGPT插件漏洞三连发之二】零点击Github仓库接管
  • Visual 使用技巧合辑
  • 栅格的着色器实现【最完善】
  • 使用AutoDL训练YOLO等计算机视觉网络模型(AutoDL+Xftp+VS Code),附详细操作步骤
  • 打开Windows来体验AIGC或者ChatGPT
  • Python使用asyncio实现异步操作
  • 深度学习系列——RNN/LSTM/GRU,seq2seq/attention机制
  • AI学习指南自然语言处理篇-Transformer模型的编码器-解码器结构
  • OpenCV通道拆分:深入理解图像处理