|
@@ -0,0 +1,47 @@
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <h1>$refs-$parent</h1>
|
|
|
+ <h3>孩子给了我{{num}}本书</h3>
|
|
|
+ <button @click='changeCar'>改变老大的车</button>
|
|
|
+ <button @click='changeHouse'>改变老二的房</button>
|
|
|
+ <button @click='saveMoney($refs)'>保管压岁钱</button>
|
|
|
+ <hr>
|
|
|
+ <hr>
|
|
|
+ <Child1 ref='c1'/>
|
|
|
+ <hr>
|
|
|
+ <hr>
|
|
|
+ <hr>
|
|
|
+ <hr>
|
|
|
+ <Child2 ref='c2'/>
|
|
|
+ <hr>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup lang='ts'>
|
|
|
+import Child1 from './Child1.vue'
|
|
|
+import Child2 from './Child2.vue'
|
|
|
+import { ref, reactive} from 'vue'
|
|
|
+let num = ref(0)
|
|
|
+let c1:any = ref();
|
|
|
+let c2:any = ref();
|
|
|
+function changeCar() {
|
|
|
+ // console.log(c1.value)
|
|
|
+ c1.value.car -= 1;
|
|
|
+}
|
|
|
+function changeHouse() {
|
|
|
+ // console.log(c1.value)
|
|
|
+ c2.value.house -= 1;
|
|
|
+}
|
|
|
+// [propName:string]:any
|
|
|
+function saveMoney(refs:{[propName:string]:any}) {
|
|
|
+ console.log(refs,'refs')
|
|
|
+ for(let key in refs) {
|
|
|
+ console.log(key,'key')
|
|
|
+ refs[key].money -= 1000;
|
|
|
+ }
|
|
|
+}
|
|
|
+defineExpose({num})
|
|
|
+</script>
|
|
|
+<style lang='scss' scoped>
|
|
|
+
|
|
|
+</style>
|