HarmonyOS componentUtils.getRectangleById 这个方法在beta5上是不是有什么改动? 我在 onAppear 获取不到相对于屏幕高度了。?

如题:HarmonyOS componentUtils.getRectangleById 这个方法在beta5上是不是有什么改动? 我在 onAppear 获取不到相对于屏幕高度了。?

阅读 442
1 个回答

这个可能是接口异步的问题,这样写就可以获取到相关内容

.onAppear(()=>{
  setTimeout(()=>{
    this.value = JSON.stringify(componentUtils.getRectangleById("image_01"))
  },0)
})

import { matrix4, componentUtils } from '@kit.ArkUI';

@Entry
@Component
struct Utils {
  @State value: string = '';

  build() {
    Column() {
      Image($r("app.media.startIcon"))

        .width(300)
        .height(100)
        .key("image_01")
        .onAppear(()=>{
          setTimeout(()=>{
            this.value = JSON.stringify(componentUtils.getRectangleById("image_01"))
          },0)
        })

      Text(this.value)
        .margin(20)
        .width(300)
        .height(300)
        .borderWidth(2)
    }.margin({left: 50})
  }
}