uni.createSelectorQuery()
需要在生命周期 mounted 后进行调用
返回一个 SelectorQuery 对象实例。可以在这个实例上使用 select 等方法选择节点,并使用 boundingClientRect 等方法选择需要查询的信息。
// util.js
// 封装获取dom的方法并导出使用
export function warpRectDom(idName) {
return new Promise(resolve => {
setTimeout(() => {
// 延时确保dom已渲染, 不使用$nextclick
let query = uni.createSelectorQuery();
// #ifndef MP-ALIPAY
query = query.in(this) // 支付宝小程序不支持in(this),而字节跳动小程序必须写in(this), 否则都取不到值
// #endif
query.select('#' + idName).boundingClientRect(data => {
resolve(data)
}).exec();
}, 20)
})
}
导入到vue的文件中使用
<template>
<view class="container" id="contId">
内容文本
</