Vue 通过什么属性获取 DOM 元素?

Vue 通过什么属性获取 DOM 元素?

阅读 2.1k
3 个回答

vue2.x

<template>
    <div ref="oBox"></div>
</template>

<script>
export default {
    mounted () {
        console.log("节点", this.$refs.oBox)
    }
}
</script>

vue3.x

<script setup>
const oBox = ref()

onMounted(function () {
    console.log("节点", oBox.value)
})
</script>

<template>
    <div ref="oBox"></div>
</template>

vue2
<template>
<div ref="targetDom"></div>
</template>
<script>
console.log(this.$refs.targetDom)
</script>

vue3
<template>
<div ref="targetDom"></div>
</template>
<script setup>
import { ref } from 'vue'
const targetDom = ref()
console.log(targetDom.value)
</script>

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题