需要是,父组件想要调用子组件的方法,并把父组件的值传给子组件。
需要用到ref ,给子组件加上ref,子组件通过defineExpore暴露出去,父组件可以使用
父组件
//父组件
<template>
<child ref='childRef '/>
<button @click="handleClick">调用子组件的方法</button>
</template>
<script setup>
import {ref,reactive} from 'vue'
import child from './child.vue'
const obj = reactive({
name: 'zhangsan',
age: 'lisi'
})
const childRef = ref()
const handleClick = ()=>{
childRef.value.setToy(obj)
}
</script>
子组件 defineExpose({ setToy });暴露出去
//子组件
<template>
子组件
</template>
<script setup>
import {ref,reactive} from 'vue'
const box = ref()
const setToy= (val)=>{
box.value = val
}
defineExpore({setToy})
</script>

4万+

被折叠的 条评论
为什么被折叠?



