Vue3| 模板引用、defineExpose宏函数

发布时间 2023-10-14 11:06:22作者: 嘎嘎鸭2

模板引用的概念:通过 ref 标识 获取真实的 dom对象或者组件实例对象

 

使用:

1. 调用 ref 函数生成一个 ref 对象

<script  setup>

import  { ref }  from  'vue'

const  h1Ref = ref (null)

</script>

2. 通过 ref 标识绑定 ref 对象到标签

<script  setup>

import  { ref }  from  'vue'

const  h1Ref = ref (null)

</script>

 

<template>

   <h1   ref = " h1Ref ">我是dom标签</h1>    // 可以通过 h1Ref.value 拿到绑定的 dom 对象(渲染完之后访问)

</template>