<div>
<slot/>
</div>
<script setup lang="ts">
const emits = defineEmits<{
(e: "loadedEvent"): void
}>()
onMounted(() => {
emits("loadedEvent")
})
</script>
<child-component @loaded-event="loadedEvent">
<div>
<div id="dom"></div>
</div>
</child-component>
<script setup lang="ts">
import ChildComponent from "@/component/ChildComponent.vue"
// 在这里处理父组件的初始化,类似于onMounted
const loadedEvent = () => {
console.log(document.getElementById("dom")) // 返回dom元素
}
// 此时这块可以处理其他逻辑,不可以直接获取dom
onMounted(() => {
console.log(document.getElementById("dom")) // 返回null
})
</script>
完结,撒花??ヽ(°▽°)ノ?