|
@@ -0,0 +1,53 @@
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <h1>第十一个</h1>
|
|
|
+ <p>我叫{{person.name}},今年{{person.age}}</p>
|
|
|
+ <p>我会{{person.desc.desc1}}和{{person.desc.desc2}}</p>
|
|
|
+ <button @click="changeName">修改名字</button>
|
|
|
+ <button @click="changeAge">修改年龄</button>
|
|
|
+ <button @click="changePart1">修改第一个</button>
|
|
|
+ <button @click="changePart2">修改第二个</button>
|
|
|
+ <button @click="changeAll">修改全部</button>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup>
|
|
|
+import { watch, reactive } from "vue";
|
|
|
+let person = reactive({
|
|
|
+ name: "孙悟空",
|
|
|
+ age: 20,
|
|
|
+ desc: {
|
|
|
+ desc1: "金箍棒",
|
|
|
+ desc2: "火眼金睛",
|
|
|
+ },
|
|
|
+});
|
|
|
+function changeName() {
|
|
|
+ person.name = "哈哈哈";
|
|
|
+}
|
|
|
+function changeAge() {
|
|
|
+ person.age = 12;
|
|
|
+}
|
|
|
+function changePart1() {
|
|
|
+ person.desc.desc1 = '吃饭'
|
|
|
+}
|
|
|
+function changePart2() {
|
|
|
+ person.desc.desc2 = '睡觉'
|
|
|
+}
|
|
|
+function changeAll() {
|
|
|
+ person.desc = {
|
|
|
+ desc1:'打羽毛球',
|
|
|
+ desc2:'打网球'
|
|
|
+ }
|
|
|
+}
|
|
|
+// 监听响应式对象的某个属性 并且所监听的值是基本数据类型时 要写成函数式
|
|
|
+// watch(()=>person.name,(newValue,oldValue)=>{
|
|
|
+// console.log(newValue,oldValue)
|
|
|
+// })
|
|
|
+// 监听响应式对象的某个属性 并且所监听的值是引用数据类型时
|
|
|
+// 如果监听对象中的属性时 可以开启深度监听
|
|
|
+// 如果不开启深度监听 监听的则是对象本身
|
|
|
+watch(()=>person.desc,(newValue,oldValue)=>{
|
|
|
+ console.log(newValue,oldValue)
|
|
|
+},{deep:true})
|
|
|
+</script>
|
|
|
+<style lang="scss" scoped></style>
|