在 TypeScript 中访问 Vue3 中已有的 ref 和 reactive 变量,需要先导入相应的 API:

import { ref, reactive } from 'vue';

然后可以使用这些变量:

const count = ref(0);
const state = reactive({
  message: 'Hello Vue!',
  count: count.value,
});

console.log(state.message);
console.log(state.count);

count.value++;
console.log(state.count);

注意:由于 ref 和 reactive 返回的是一个包装后的对象,因此需要使用 .value 属性来访问其值。

Vue3 中 TypeScript 访问 Ref 和 Reactive 变量

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

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