借助 ref() 函数
通过 ref 函数,可以实现类似 this.$refs 的功能。
1.首先在 setup 中定义一个 Ref 变量
import {ref, onMounted} from 'vue'
export default{
setup() {
const Ref = ref()
onMounted(() => {
console.log(Ref.value)
})
return {Ref}
}
}
2.将这个 Ref 变量挂载到 DOM 上
<template>
<div ref="Ref" />
</template>
当 onMount 钩子被触发的时候,div 的 DOM 会在控制台打印出来。