Skip to content

2.12 模板引用

html
<input ref="input">

2.12.1 访问模板引用

如果你想在模板中的表达式上访问 input,在初次渲染时会是 null

vue
<script setup>
import { ref, onMounted } from 'vue'

// 声明一个 ref 来存放该元素的引用
// 必须和模板里的 ref 同名
const input = ref(null)

onMounted(() => {
  input.value.focus()
})
</script>

<template>
  <input ref="input" />
</template>

侦听一个模板引用 ref 的变化,确保考虑到其值为 null 的情况

js
watchEffect(() => {
  if (input.value) {
    input.value.focus()
  } else {
    // 此时还未挂载,或此元素已经被卸载(例如通过 v-if 控制)
  }
})

2.12.2 v-for中的模板引用

vue
<script setup>
import { ref, onMounted } from 'vue'

const list = ref([
  /* ... */
])

const itemRefs = ref([])

onMounted(() => console.log(itemRefs.value))
</script>

<template>
  <ul>
    <li v-for="item in list" ref="itemRefs">
      {{ item }}
    </li>
  </ul>
</template>

2.12.3 函数模板引用

html
<input :ref="(el) => { /* 将 el 赋值给一个数据属性或 ref 变量 */ }">

2.12.4 组件上的ref

vue
<script setup>
import { ref, onMounted } from 'vue'
import Child from './Child.vue'

const child = ref(null)

onMounted(() => {
  // child.value 是 <Child /> 组件的实例
})
</script>

<template>
  <Child ref="child" />
</template>