uniapp + vue3.2 开发小程序 用ref获取DOM元素一直为null
尝试了以下几种方案:

  1. onMounted()打印不行
  2. onReady()打印不行
  3. nextTrick()打印不行
  4. await 不行
  5. 不在view上绑定ref,在其他标签上绑定不行
  6. 最后结论:引入子组件,给子组件绑定ref才能获取到?

有人遇到同款问题并已解决的看到捞一下我…

<template>
	<view class="index">
		<view class="index_navbar" ref="navBar">
			huuhu
		</view>
	</view>
</template>

<script setup>
	import { ref, onMounted, reactive, nextTick } from "vue";
	const navBar = ref(null)
	onMounted(() => {
		getMenuBtnClient();
	})
	function getMenuBtnClient() {
		const btn_client = wx.getMenuButtonBoundingClientRect()
		console.log(btn_client)
		console.log(navBar.value) // 为null
		// navbar.style.top = btn_client.top
		// navbar.style.left = btn_client.right
	}
</script>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐