import { ref } from 'vue'
const count = ref(0)
<div>{{count}}</div>
console.log(count) console.log(count.value)
<span @click="changeTheme" :class="['iconfont', darkMode ? 'icon-Sun' : 'icon-Moon-Star']"></span>
import { ref } from 'vue' const darkMode = ref(false) const changeTheme = () => { darkMode.value = !darkMode.value }
import { reactive } from 'vue'
const obj = reactive({ id: 10, name: 'gz' })
<div>{{obj.id}} - {{obj.name}}</div>
const done = () => { obj.id = 20 } console.log(obj)
const count = reactive(10)
const done = () => { obj = { id: 20, name: 'gl' } }
const { id, name } = obj
. 创建简单的响应式变量时,当然只能 ref()
. 如果需要简单的对象,ref()、reactive() 都可以
. 层级比较深的对象,请使用 reactive()