Vue懶載入的學習
阿新 • • 發佈:2018-11-27
文章參考
Vue官方寫法
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export function createRouter () {
return new Router({
mode: 'history',
routes: [
{ path: '/', component: () => import('./components/Home.vue' ) },
{ path: '/item/:id', component: () => import('./components/Item.vue') }
]
})
}
commonjs寫法
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export function createRouter () {
return new Router({
mode: 'history',
routes: [
{ path: '/', component: resolve => require(['./components/Home.vue'],resolve) },
{ path: '/item/:id', component: resolve => require(['./components/Item.vue'],resolve) }
]
})
}
使用建議
建議使用Vue官方推薦的使用方法,如果對第二種commonJS寫法不理解,就需要了解Nodejs中require的用法