Answer the question
In order to leave comments, you need to log in
How to asynchronously import child components into parent in vue?
Please tell me: there is the following component
<template>
<div class="homePage">
<header class="header">
<HeaderComponent></HeaderComponent>
</header>
<div class="homePage__block">
<div class="homePage__account">
<account></account>
</div>
</div>
</div>
<template>
<script>
export default {
components: {
HeaderComponent: () => import('../layout/header-main.vue'),
},
mounted () {
console.log("Test")
}
}
</script>
Answer the question
In order to leave comments, you need to log in
Well, this is the point of loading components asynchronously, the parent will not wait until the child asynchronous component is loaded and will call mounted . I see several options.
1. Load the component synchronously.
2. Use $
<template>
<p>Child component</p>
</template>
<script>
export default {
mounted() {
//имитируем задержку
setTimeout(() => {
this.$emit('load');
}, 2000);
}
}
</script>
<template>
<h1>Parent component</h1>
<child @load="onLoad"></child>
</template>
<script>
export default {
components: {
child: () => import('child.vue'),
},
methods: {
onLoad() {
console.log('Child loaded!');
}
}
}
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question