codercup / unibest

unibest - 最好用的 uniapp 开发框架。unibest 是由 uniapp + Vue3 + Ts + Vite4 + UnoCss + UniUI 驱动的跨端快速启动模板,使用 VS Code 开发,具有代码提示、自动格式化、统一配置、代码片段等功能,同时内置了大量平时开发常用的基本组件,开箱即用,让你编写 uniapp 拥有 best 体验。
https://codercup.github.io/unibest-docs/
MIT License
1.31k stars 174 forks source link

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

Open wll8 opened 2 months ago

wll8 commented 2 months ago

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 。

codercup commented 2 months ago

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

wll8 commented 2 months ago

试过 1s,不得行的。