vue異步組件技術 ==== 異步加載
vue-router配置路由 , 使用vue的異步組件技術 , 能夠實現按需加載 .
可是,這種狀況下一個組件生成一個js文件vue
/* vue異步組件技術 */ { path: '/home', name: 'home', component: resolve => require(['@/components/home'],resolve) },{ path: '/index', name: 'Index', component: resolve => require(['@/components/index'],resolve) },{ path: '/about', name: 'about', component: resolve => require(['@/components/about'],resolve) }
組件懶加載方案二 路由懶加載(使用import)webpack
// 下面2行代碼,沒有指定webpackChunkName,每一個組件打包成一個js文件。 /* const Home = () => import('@/components/home') const Index = () => import('@/components/index') const About = () => import('@/components/about') */ // 下面2行代碼,指定了相同的webpackChunkName,會合並打包成一個js文件。 把組件按組分塊 const Home = () => import(/* webpackChunkName: 'ImportFuncDemo' */ '@/components/home') const Index = () => import(/* webpackChunkName: 'ImportFuncDemo' */ '@/components/index') const About = () => import(/* webpackChunkName: 'ImportFuncDemo' */ '@/components/about')
{ path: '/about', component: About }, { path: '/index', component: Index }, { path: '/home', component: Home
}
webpack提供的require.ensure()
vue-router配置路由,使用webpack的require.ensure技術,也能夠實現按需加載。
這種狀況下,多個路由指定相同的chunkName,會合並打包成一個js文件。web
/* 組件懶加載方案三: webpack提供的require.ensure() */ { path: '/home', name: 'home', component: r => require.ensure([], () => r(require('@/components/home')), 'demo') }, { path: '/index', name: 'Index', component: r => require.ensure([], () => r(require('@/components/index')), 'demo') }, { path: '/about', name: 'about', component: r => require.ensure([], () => r(require('@/components/about')), 'demo-01') }
參考:https://www.jianshu.com/p/876e1b85adb6vue-router