<template>
<div class="container">
<div>
<p>count的值:{{count}}</p>
<button @click="add">改数据</button>
</div>
<hr>
<div>
<p>{{obj.name}}</p>
<p>{{obj.age}}</p>
<p>{{obj.brand.name}}</p>
<button @click="updateName">改名字</button>
<button @click="updateBrandName">改品牌名字</button>
</div>
</div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
export default {
name: 'Home',
setup() {
const count = ref(0)
const add = () => {
count.value++
}
const obj = reactive({
name: 'ls',
age: 10,
brand: {
id: 1,
name: '宝马'
}
})
const updateName = () => {
obj.name = 'zs'
}
const updateBrandName = () => {
obj.brand.name = '奔驰'
}
watch(obj, ()=>{
console.log('数据改变了')
})
watch(obj.brand.name, (newValue, oldValue) => {
console.log('The new counter value is: ' + obj.brand.value)
console.log("new counter======{}==",newValue);
console.log("old counter======{}==",oldValue);
})
watch(()=>obj.brand, ()=>{
console.log('brand数据改变了')
console.log()
},{
deep: true,
immediate: true
})
return {count, add, obj, updateName, updateBrandName}
}
}
</script>