getBoundingClientRect报错
时间: 2023-10-17 11:35:39 浏览: 41
根据引用,报错"TypeError: this.$refs[refName].getBoundingClientRect is not a function"是因为在ref所引用的元素上调用getBoundingClientRect方法时出错。具体原因可能是因为在该元素上没有定义getBoundingClientRect方法。
根据引用和引用,正确的做法是将ref写在原生的HTML标签上,而不是组件上。即使是相同的ref名称,如果将ref定义在组件上,再使用getBoundingClientRect方法获取top的话,也会报错。因此,正确的写法是将ref定义在原生的HTML标签上,如<div ref="QZBLGZH"></div>,然后使用this.$refs.QZBLGZH.getBoundingClientRect().top来获取top值。这样就不会报错了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [vue中使用getBoundingClientRect()获取元素位置报错](https://blog.csdn.net/m0_38010595/article/details/112188554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]