Created
October 29, 2020 18:25
-
-
Save Aslam97/84642ded1a959530bf03080ed092eb0d to your computer and use it in GitHub Desktop.
Vue dynamic import
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
// 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