小程序獲能用createSelectorQuery,如果是子組件,后面可以額外加一個參數in來指定獲取dom的范圍。小程序里面可以直接.in(this),但是vue3沒有this了,那就只能通過getCurrentInstance去獲取當前實例代替this ,注意這里需要用解構的方法把proxy引進來
<view class="image-container">內容</view>import { ref, reactive, onMounted, nextTick, watch, getCurrentInstance } from 'vue';
const { proxy } = getCurrentInstance();
// 獲取容器尺寸const getContainerSize = () => {new Promise((resolve, reject) => {let view = uni.createSelectorQuery().in(proxy);view.select('.image-container').boundingClientRect((data) => {containerWidth.value = data.width;containerHeight.value = data.height;console.log(data);resolve(data);}).exec();}).then((res) => {});};/ 組件掛載后初始化onMounted(async () => {await nextTick();getContainerSize();});