|
|
@@ -0,0 +1,69 @@
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <h1>Demo14:watch监听reactive、ref定义的引用数据类型中多个属性</h1>
|
|
|
+ <h2>我在{{ flower.month }}的{{ flower.city }}--{{ flower.a.b }}</h2>
|
|
|
+ <button @click="changeMonth">修改月份</button>
|
|
|
+ <button @click="changeB">修改b</button>
|
|
|
+ <button @click="changeFlower">修改整体</button>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script lang="ts" setup>
|
|
|
+import { ref, watch } from "vue";
|
|
|
+let flower = ref({
|
|
|
+ month: "六月",
|
|
|
+ city: "哈尔滨",
|
|
|
+ a: {
|
|
|
+ b: 1,
|
|
|
+ },
|
|
|
+});
|
|
|
+function changeB() {
|
|
|
+ flower.value.a.b = 12;
|
|
|
+}
|
|
|
+function changeFlower() {
|
|
|
+ // flower.value--;
|
|
|
+ Object.assign(flower.value, {
|
|
|
+ month: "八月",
|
|
|
+ city: "北京",
|
|
|
+ a: {
|
|
|
+ b: "122",
|
|
|
+ },
|
|
|
+ });
|
|
|
+}
|
|
|
+function changeMonth() {
|
|
|
+ flower.value.month = "七月";
|
|
|
+}
|
|
|
+// watch(
|
|
|
+// flower.value.a,
|
|
|
+// (newValue, oldValue) => {
|
|
|
+// console.log(newValue, oldValue, "12");
|
|
|
+// },
|
|
|
+// {
|
|
|
+// deep: true,
|
|
|
+// immediate: true
|
|
|
+// }
|
|
|
+// );
|
|
|
+// watch(
|
|
|
+// ()=>flower.value.month,
|
|
|
+// (newValue, oldValue) => {
|
|
|
+// console.log(newValue, oldValue, "12");
|
|
|
+// },
|
|
|
+// {
|
|
|
+// deep: true,
|
|
|
+// immediate: true,
|
|
|
+// }
|
|
|
+// );
|
|
|
+watch(
|
|
|
+ [()=>flower.value.month,flower.value.a],
|
|
|
+ (newValue, oldValue) => {
|
|
|
+ console.log(newValue, oldValue, "哈哈");
|
|
|
+ },
|
|
|
+ {
|
|
|
+ deep: true,
|
|
|
+ immediate: true,
|
|
|
+ }
|
|
|
+);
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+</style>
|