1. rective 创建一个响应式对象
2. ref 和 reactive
- vue 使用 proxy API创建响应式,但是 proxy 只能代理对象。
- ref 使用 defined
- reactive 使用 proxy
- ref 实际调用了 reactive API
此时产生的问题:
- reactive 性能更好,应该推荐用户使用 reactive。
- ref 实际调用 reactive,是不是代表:只需要 ref
2025/11/30小于 1 分钟
// useCount.ts
import { ref, watch } from 'vue'
const count = ref(0)
const doubleCount = ref(0)
function handleChangeCount() {
count.value++
}
watch(count, () => {
doubleCount.value = count.value * 2
})
export function useCounter() {
return {
count,
handleChangeCount,
doubleCount,
}
}
<template>
<div>{{ num }}</div>
</template>
<script setup>
import { ref } from 'vue'
const num = ref(0)
</script>
两种方式
npm create vue@latest
实际区别(Vite 官方逻辑):
assets/public//xxx.png最基本的数据绑定形式是文本插值,它使用的是“Mustache”语法 (即双大括号)
<template>
<div>{{ name }}</div>
</template>
<script setup>
const name = "lorem";
</script>
npm install swiper
// import Swiper JS
import Swiper from 'swiper';
// import Swiper styles
import 'swiper/css';