Skip to content

Instantly share code, notes, and snippets.

@Aslam97
Created October 29, 2020 18:25
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 Aslam97/84642ded1a959530bf03080ed092eb0d to your computer and use it in GitHub Desktop.
Save Aslam97/84642ded1a959530bf03080ed092eb0d to your computer and use it in GitHub Desktop.
Vue dynamic import
// Lazy-loads view components, but with better UX. A loading view
// will be used if the component takes a while to load, falling
// back to a timeout view in case the page fails to load. You can
// use this component to lazy-load a route with:
//
// component: () => lazyLoadView(import('@views/my-view'))
//
// NOTE: Components loaded with this strategy DO NOT have access
// to in-component guards, such as beforeRouteEnter,
// beforeRouteUpdate, and beforeRouteLeave. You must either use
// route-level guards instead or lazy-load the component directly:
//
// component: () => importView('path/file.vue')
//
function importView(path) {
return () =>
import(
/* webpackChunkName: '' */
/* webpackPrefetch: true */
`@views/${path}`
).then(module => module.default || module);
}
function lazyLoadView(AsyncView) {
const AsyncHandler = () => ({
component: AsyncView,
loading: require('@views/_loading.vue').default,
delay: 400,
error: require('@views/_timeout.vue').default,
timeout: 10000,
})
return Promise.resolve({
functional: true,
render(h, { data, children }) {
return h(AsyncHandler, data, children)
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment