|
|
@@ -0,0 +1,57 @@
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <h1>watch:ref/reactive引用数据类型中的多个属性</h1>
|
|
|
+ <h2>我有{{ flower.num }}朵{{flower.type}}花--新的:{{ flower.aaa.b }}-- {{ flower.aaa.c }}</h2>
|
|
|
+ <button @click="changeNum">减少数量</button>
|
|
|
+ <button @click="changeFlower">修改全部</button>
|
|
|
+ <button @click="changeType">修改品类</button>
|
|
|
+ <button @click="changeC">修改C</button>
|
|
|
+ <button @click="changeA">修改AAA</button>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script lang="ts" setup>
|
|
|
+import {ref, watch} from "vue"
|
|
|
+let flower = ref({
|
|
|
+ type:"牡丹",
|
|
|
+ num: 10,
|
|
|
+ aaa:{
|
|
|
+ b:12,
|
|
|
+ c: 90
|
|
|
+ }
|
|
|
+});
|
|
|
+function changeA() {
|
|
|
+ flower.value.aaa = {
|
|
|
+ b: 1,
|
|
|
+ c: 2
|
|
|
+ }
|
|
|
+}
|
|
|
+function changeFlower() {
|
|
|
+ Object.assign(flower.value,{
|
|
|
+ type:'康乃馨',
|
|
|
+ num: 20,
|
|
|
+ aaa:{
|
|
|
+ b:3,
|
|
|
+ c:4
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+const changeNum = () => {
|
|
|
+ flower.value.num--;
|
|
|
+}
|
|
|
+const changeC = () => {
|
|
|
+ flower.value.aaa.c--;
|
|
|
+}
|
|
|
+const changeType = () => {
|
|
|
+ flower.value.type = '丁香'
|
|
|
+}
|
|
|
+watch([()=>flower.value.aaa.b,() => flower.value.num],(newValue)=>{
|
|
|
+ console.log(newValue)
|
|
|
+},{
|
|
|
+ deep: true,
|
|
|
+ immediate: true
|
|
|
+})
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+</style>
|