1、ES6提供的import()
// 下面3行代码,没有指定webpackName,每个组件打包成一个js文件。
const Home = () => import('@/components/home')
const Index = () => import('@/components/index')
const About = () => import('@/components/about')
// 下面3行代码,指定了相同的webpackName,会合并打包成一个js文件。 把组件按组分块
const Home = () => import( /* webpackName: 'ImportFuncDemo' */ '@/components/home')
const Index = () => import( /* webpackName: 'ImportFuncDemo' */ '@/components/index')
const About = () => import( /* webpackName: 'ImportFuncDemo' */ '@/components/about')
{
path: '/about',
component: About
}, {
path: '/index',
component: Index
}, {
path: '/home',
component: Home
}
2、Vue官方的require()
使用vue的异步组件技术,可以实现按需加载。但是,这种情况下一个组件生成一个js文件。
/* 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)
}
3、Webpack提供的require(),ensure()
// 多个路由指定相同的webpackName,会合并打包成一个js文件。
{
path: '/home',
name: 'home',
component: r => require.ensure([], () => r(require('@/components/home')), 'webpackName')
}, {
path: '/index',
name: 'Index',
component: r => require.ensure([], () => r(require('@/components/index')), 'webpackName')
}, {
path: '/about',
name: 'about',
component: r => require.ensure([], () => r(require('@/components/about')), 'webpackName')
}
网友评论