<template>
<div>
<h1> 当前数据{{ count }}</h1>
<span>对象数据 {{ objDatas.name }}</span>
<p ></p>
<el-input v-model="fromDatas.name" placeholder="Please input" @change="fromChange"/>
<el-button type="primary" @click="changeAdd" >+</el-button>
<Messages auType ='外层type' @getChild = 'handleGetChild' ref='chRef'></Messages>
</div>
</template>
<script setup>
import Messages from '../messages/index.vue'
import { ref ,reactive ,watchEffect,provide ,onMounted,getCurrentInstance} from 'vue'
let emit = defineEmits()
let count = ref(1)
let objDatas = reactive({
name:'王'
});
let fromDatas = reactive({
name:'这是名称'
});
let chRef = ''
onMounted(()=>{
chRef = getCurrentInstance();
})
let handleGetChild = val=>{
console.log("获取子组件的数据",val)
}
let changeAdd = ()=>{
count.value = count.value+1;
objDatas.name = '修改后的姓名';
console.log("chRef", chRef.ctx.$refs.chRef)
}
watchEffect(() => {
console.log("组件更新",count.value);
})
let fromChange = (value)=>{
console.log("djkkdj",value);
console.log("数据更新",fromDatas)
}
provide('homeDatas',{
homeDatas:{
name:"外层组件数据"
}
})
</script>
<style>
</style>
<template>
<div>子组件</div>
<el-button type="primary" @click="changeAdd" >子组件触发函数</el-button>
</template>
<script setup>
import { inject,defineExpose } from 'vue';
let emit = defineEmits()
let props = defineProps({
auType:String
})
let injects = inject('homeDatas')
console.log("injects",injects);
console.log("props,context",props,)
const changeAdd = ()=>{
emit('getChild',{
a:1,age:12
})
}
defineExpose({
getChildDatas(){
return 22
}
})
</script>
<style>
</style>