unibest icon indicating copy to clipboard operation
unibest copied to clipboard

layouts 中获取节点,小程序环境无法获取

Open wll8 opened this issue 1 year ago • 3 comments
trafficstars

Bug report(问题描述)

layouts 中获取节点,小程序环境无法获取

h5 可以。

Steps to reproduce(问题复现步骤)

代码

image

/**
 * 获取节点信息
 * @param selector 节点选择器 #id,.class
 * @param all 是否返回所有 selector 对应的节点
 * @param scope 作用域(支付宝小程序无效)
 * @returns 节点信息或节点信息数组
 */
export function getRect<T extends boolean>(selector: string, all: T, scope?: any): Promise<RectResultType<T>> {
  return new Promise<RectResultType<T>>((resolve, reject) => {
    let query: UniNamespace.SelectorQuery | null = null
    if (scope) {
      query = uni.createSelectorQuery().in(scope)
    } else {
      query = uni.createSelectorQuery()
    }
    query[all ? 'selectAll' : 'select'](selector)
      .boundingClientRect((rect) => {
        if (all && isArray(rect) && rect.length > 0) {
          resolve(rect as RectResultType<T>)
        } else if (!all && rect) {
          resolve(rect as RectResultType<T>)
        } else {
          reject(new Error('No nodes found'))
        }
      })
      .exec()
  })
}

h5 输出

image

小程序输出

image

env (环境)

使用的当前仓库的 demo 分支 3bf4ea2f0da1886c49bc73966eb8648fc802a975 。

  • node v18.19.0
  • pnpm 8.12.1
  • vscode 1.87.2
  • os win10
  • hbx 4.08

wll8 avatar Apr 15 '24 09:04 wll8

1)试试加大延时,比如 setTimeout 2s 这样

codercup avatar Apr 16 '24 05:04 codercup

试过 1s,不得行的。

wll8 avatar Apr 16 '24 05:04 wll8

在用户操作事件里面也没法获取,这个有什么解决方案吗,因为组合式API 不能使用 this, getCurrentInstance 方法返回也是 null, 除非 使用 option 方式,不然很难获取节点信息

szmxx avatar Aug 29 '24 06:08 szmxx