子组件使用defineExpose暴露出去
子组件testIndex.vue
<template>
<div>
{{num}}
</div>
</template>
<script setup>
import {ref} from "vue";
const count= ref(0);
const num = ref(0);
function childMethod() {
console.log('我是子组件');
num.value+=5
}
defineExpose({
count,
num,
childMethod
});
</script>
父组件
<template>
<el-button @click="test">点击改变父组件的值和方法</el-button>
<TestIndex ref="V"></TestIndex>
</tempalte>
<script setup>
import { ref } from "vue";
import TestIndex from "./testIndex.vue"
const V=ref(null)
const test=()=>{
const {count} = V.value
V.value.count++;
V.value.childMethod();
}
<script>