2
<template>
    <div>
        <AsyncComponent1></AsyncComponent1>
        <!-- Render AsyncComponent2 after AsyncComponent1 -->
        <AsyncComponent2></AsyncComponent2>
    </div>
</template>

<script>
export default {
    name: "My Component"
    components: {
        AsyncComponent1: () => import("./AsyncComponent1"),
        AsyncComponent2: () => import("./AsyncComponent2")
    }
};
</script>

I'm loading two components asynchronously within a component but I need one of the components to render after the other. I wonder if thats possible?

2 Answers 2

1

Add a v-if to a ref in the other component.

<template>
   <div>
       <AsyncComponent1 ref="c1"></AsyncComponent1>
       <!-- Render AsyncComponent2 after AsyncComponent1 -->
       <AsyncComponent2 v-if="$refs.c1"></AsyncComponent2>
   </div>
</template>
Sign up to request clarification or add additional context in comments.

Comments

1

You could have the first component emit an event, that is listened to by the parent and used to toggle the second component

<template>
    <div>
        <AsyncComponent1 v-on:loaded="componentLoaded"></AsyncComponent1>
        <!-- Render AsyncComponent2 after AsyncComponent1 -->
        <AsyncComponent2 v-if="hasComponent"></AsyncComponent2>
    </div>
</template>

<script>
export default {
    name: "My Component",
    components: {
        AsyncComponent1: () => import("./AsyncComponent1"),
        AsyncComponent2: () => import("./AsyncComponent2")
    },
    data: {
        hasComponent: false
    },
    methods: {
        componentLoaded() {
            this.hasComponent = true;
        }
    }
};
</script>

And then in AsyncComponent1.vue:

...
mounted() {
    this.$emit("loaded");
}
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.