在ts中访问vue3中的ref和reactive变量,需要先导入Vue3的相关模块:

import {ref, reactive} from 'vue'

然后可以使用ref和reactive函数创建变量:

const count = ref(0) // 创建一个ref变量
const user = reactive({name: 'Tom', age: 18}) // 创建一个reactive变量

访问变量的值可以使用.value属性(ref)或直接访问属性(reactive):

console.log(count.value) // 输出0
console.log(user.name) // 输出'Tom'

对于reactive变量,可以使用isRef函数判断是否是ref变量:

import {isRef} from 'vue'

console.log(isRef(count)) // 输出true
console.log(isRef(user)) // 输出false
ts中如何访问到vue3中ref和reavtive变量

原文地址: https://www.cveoy.top/t/topic/b1lN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录