元素上使用 ref属性关联响应式数据,获取DOM元素
1.创建ref
const hRef = ref(null)
2.模板中建立关联
<h1 ref="hRef">我是标题</h1>
3.使用
hRef.value
<script setup>
import { ref } from 'vue'
const hRef = ref(null)
const clickFn = () => {
hRef.value.innerText = '我不是标题'
}
</script>
<template>
<div>
<h1 ref="hRef">我是标题</h1>
<button @click="clickFn">操作DOM</button>
</div>
</template>