需求,在页面中,有时需要获取渲染组件的长宽信息,可以使用select
选择器进行查询,直接上代码如下:
<div ref="tabbar" id="tabbar" class="x-tabbar-wrap">
</div>
const getTabbarHeight = () => {
const { windowWidth, safeAreaInsets } = <any>uni.getSystemInfoSync();
const ratio = 750 / windowWidth;
safeAreaInsetsBottom.value = safeAreaInsets.bottom * ratio;
// 获取组件实例
const instance = getCurrentInstance();
const query = uni.createSelectorQuery().in(instance);
query
.select("#tabbar")
.boundingClientRect((rect) => {
console.log("rect", rect);
if (rect) {
console.log("rect", rect);
}
})
.exec();
};