Skip to content

Instantly share code, notes, and snippets.

@nabrown
Last active March 22, 2020 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabrown/20f7fa827438e400473ff33df666443f to your computer and use it in GitHub Desktop.
Save nabrown/20f7fa827438e400473ff33df666443f to your computer and use it in GitHub Desktop.
Basic asynchronous component import in Vue single file component
<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