<child-component ref="myChild" :count="countNum" @add="handleClick "></child-component>
<script>
import ChildComponent from './ChildComponent.vue'
imort { reactive, toRefs } from 'vue'
export default {
compontens: {
ChildComponent
},
setup() {
const state = reactive({ countNum: 1 })
const handleClick = (num) => {
state.countNum += num
}
const add = () => {
state.countNum += 1
}
const myChild= ref()
const clickChildCom = () => {
myChild.value.changeText()
}
return {
...toRefs(state),
handleClick,
add,
clickChildCom
}
}
}
</script>
<template>
<div>父组件传过来的值:{{ count }}</div>
<el-button @click="changeCount "></el-button>
</template>
<script>
export default {
props: {
count: Number
},
setup(props, ctx) {
const text= ref("子组件");
const changeText = () =>{
text.value = "改变子组件";
}
const changeCount = () => {
ctx.emit('add', 2)
}
return {
changeCount,
text,
changeText
}
}
}
</script>