(三)Ref 类型定义

老怪兽2022年11月30日
  • TypeScript
  • Ts和Vue
小于 1 分钟

一、定义 ref 类型的三种方式

  1. 自动推断
<script setup>
import { ref } from 'vue'

const num = ref(0)
</script>

  1. 显示定义类型(需要导入 Ref)
<script setup>
import { ref, Ref } from 'vue'

const str: Ref<string> = ref('str')
</script>
  1. 直接在 ref 后面加上泛型
<script setup>
import { ref } from 'vue'

const bool = ref<boolean>(true)
</script>

总结-写在最后

说明

refreactive 定义类型是一样的

Loading...