VUE3 ref

用于创建可以使用任何值类型的响应式对象

<script setup>
import { ref } from 'vue'
const count = ref(0)
const handleCount = function(){
  count.value++
}
</script>

<template>
  <span>{{count}}</span>
  <button @click="handleCount">加一</button>
</template>

用于做模板引用

  1. 使用选项式 api

<script>
export default {
  mounted() {
    this.$refs.inputRef.focus()
  }
}
</script>

<template>
  <input ref="inputRef" />
</template>
  1. 使用组合式 api

使用动态生成的 ref

Last updated

Was this helpful?