养成一个习惯,vue操作组件的时候,ref命名的一定要加ref为后缀名
ref属性
- 被用来给元素或子组件注册引用信息(id的替代者)
- 应用在html标签上获取的是真实DOM元素,应用在组件标签上获取的是组件实例对象(vc)
Vue2---使用方式:
打标识:<h1 ref="名称"></h1>
获取:this.$refs.名称
Vue3---使用方式
<template>
<button ref="buttonRef">年纪</button>
</template>
<script setup lang="ts">
// 标记Ref
const buttonRef = ref(null)
console.log(buttonRef.value)
</script>