Last active
March 22, 2020 22:18
-
-
Save nabrown/20f7fa827438e400473ff33df666443f to your computer and use it in GitHub Desktop.
Basic asynchronous component import in Vue single file component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<button v-on:click="showChild = !showChild">Toggle Child Component!</button> | |
<child-component v-if="showChild" message="I am the child component."></child-component> | |
</div> | |
</template> | |
<script> | |
import LoadingState from '@/components/LoadingState' | |
import ErrorState from '@/components/ErrorState' | |
export default { | |
name: 'ParentComponent', | |
components: { | |
LoadingState, | |
ErrorState, | |
ChildComponent: () => ({ | |
// the component we want to lazy load | |
component: import('@/components/ChildComponent'), | |
// the component to show if our ChildComponent is taking some time to load | |
loading: LoadingState, | |
// the component to show if ChildComponent fails to load | |
error: ErrorState, | |
// the time to wait before showing LoadingState | |
delay: 200, | |
// the time to wait before we give up and show ErrorState | |
timeout: 1500 | |
}) | |
}, | |
data(){ | |
return { | |
showChild: false | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment