|
|
@@ -0,0 +1,46 @@
|
|
|
+<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>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script lang="ts" setup>
|
|
|
+import {ref, watch} from "vue"
|
|
|
+let flower = ref({
|
|
|
+ type:"牡丹",
|
|
|
+ num: 10,
|
|
|
+ aaa:{
|
|
|
+ b:12,
|
|
|
+ c: 90
|
|
|
+ }
|
|
|
+});
|
|
|
+function changeFlower() {
|
|
|
+ Object.assign(flower.value,{
|
|
|
+ type:'康乃馨',
|
|
|
+ num: 20
|
|
|
+ })
|
|
|
+}
|
|
|
+const changeNum = () => {
|
|
|
+ flower.value.num--;
|
|
|
+}
|
|
|
+const changeC = () => {
|
|
|
+ flower.value.aaa.c--;
|
|
|
+}
|
|
|
+const changeType = () => {
|
|
|
+ flower.value.type = '丁香'
|
|
|
+}
|
|
|
+watch(()=>flower.value.num,(newValue,oldValue)=>{
|
|
|
+ console.log(newValue,oldValue)
|
|
|
+},{
|
|
|
+ deep: true,
|
|
|
+ immediate: true
|
|
|
+})
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+</style>
|