Toronto 16th Nov 2018
Vue core team
Remote Freelance Developer
ย
import('a_package').then(module => module.default)
import('./utils.js').then(module => module.log)
const aPackage = () => import('a_package')
const utils = () => import('./utils.js')
SplitChunksPlugin
๐Docs
import(/* webpackPrefetch: true */ 'LoginModal')
<link rel="prefetch" href="login-modal-chunk.js" />
import(/* webpackPreload: true */ 'ChartingLibrary')
<link rel="preload" href="chunk.js" />
import Calendar from '@/components/Calendar.vue'
const Calendar = () => import('@/components/Calendar.vue')
// router.js
const Home = () => import('@/views/Home')
const User = () => import('@/views/User')
const routes = [
{ path: '/', component: Home },
{ path: '/users/:id', component: User },
]
// App.vue
const Search = () => import('@/components/Search')
export default {
components: { Search }
}
Search.vue
PROFIT ๐ฅ
<component :is="dynamicComponent"/>
// inside of a component method
// local or global component
this.dynamicComponent = 'UserCard'
// component descriptor
// import UserCard from './UserCard'
this.dynamicComponent = UserCard
export default {
render (h) {
return h(this.dynamicComponent)
},
methods: {
changeDisplayedComponent () {
this.dynamicComponent = 'UserCard'
// or
this.dynamicComponent = UserCard
}
}
}
import Calendar from '@/components/Calendar.vue'
const Calendar = () => import('@/components/Calendar.vue')
<component :is="dynamicComponent"/>
// inside of a component method
this.dynamicComponent = () => import('./UserCard')
render(h) { return h(this.dynamicComponnet) }
import(`@/components/async/${name}.vue`)
let webpack creates chunks ๐
import(myComponentPath) // ๐ฑ โ ๏ธ
your app
import(myComponentPath) // ๐ฑ โ ๏ธ
import(`@/components/Lazy${name}.vue`) // ๐
import(`@/components/async/${name}.vue`) // ๐ฏ
<ExamplePreview name="counter"/>
computed: {
demoComponent() {
// make demoComponent depend on name
const name = this.name
return () =>
import(`@docs/examples/${name}/index.js`)
}
}
<component :is="demoComponent"/>
when your app is unmaintainable but you still care
vue ui
// in a component method
this.error = null
return import('./Component.vue').catch(err => {
this.error = err
})
<p class="danger" v-if="error">{{ error }}</p>
this.error = null
this.pending = true
return import('./Component.vue').then(module => {
this.pending = false
return module
}).catch(err => this.error = err)
<p class="danger" v-if="error">{{ error }}</p>
<p class="info" v-else-if="pending">Loading...</p>
when you realize you have been writing the same thing over and over
const AsyncComponent = () => ({
component: import('./MyComponent.vue'),
loading: LoadingComponent,
error: ErrorComponent,
delay: 200,
timeout: 3000
})
<Promised :promise="componentPromise">
<p slot="pending">Loading component</p>
<component
slot-scope="module"
:is="module.default"
/>
<p slot="rejected" slot-scope="error">
{{ error.message}}
</p>
</Promised>
๐ Github repo