当父组件想直接调用父组件的属性或者方法时,子组件可以使用defineExpose暴露自身的属性或者方法,父组件中使用ref调用子组件暴露的属性或方法。同样 子组件 可以通过 parent 获取父组件信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <template> <div class="box"> <h1>我是父亲曹操:{{money}}</h1> <button @click="handler">找我的儿子曹植借10元</button> <hr> <Son ref="son"></Son> <hr> <Dau></Dau> </div> </template>
<script setup lang="ts"> //ref:可以获取真实的DOM节点,可以获取到子组件实例VC //$parent:可以在子组件内部获取到父组件的实例 //引入子组件 import Son from './Son.vue' import Dau from './Daughter.vue' import {ref} from 'vue'; //父组件钱数 let money = ref(100000000); //获取子组件的实例 let son = ref(); //父组件内部按钮点击回调 const handler = ()=>{ money.value+=10; //儿子钱数减去10 son.value.money-=10; son.value.fly(); } //对外暴露 defineExpose({ money }) </script>
<style scoped> .box{ width: 100vw; height: 500px; background: skyblue; } </style>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <template> <div class="dau"> <h1>我是闺女曹杰{{money}}</h1> <button @click="handler($parent)">点击我爸爸给我10000元</button> </div> </template>
<script setup lang="ts"> import {ref} from 'vue'; //闺女钱数 let money = ref(999999); //闺女按钮点击回调 const handler = ($parent)=>{ money.value+=10000; $parent.money-=10000; } </script>
<style scoped> .dau{ width: 300px; height: 300px; background: hotpink; } </style>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <template> <div class="son"> <h3>我是子组件:曹植{{money}}</h3> </div> </template>
<script setup lang="ts"> import {ref} from 'vue'; //儿子钱数 let money = ref(666); const fly = ()=>{ console.log('我可以飞'); } //组件内部数据对外关闭的,别人不能访问 //如果想让外部访问需要通过defineExpose方法对外暴露 defineExpose({ money, fly }) </script>
<style scoped> .son { width: 300px; height: 200px; background: cyan; } </style>
|