When building apps with a bundler, the JavaScript bundle can become quite large, and thus affect the page load time. It would be more efficient if we can split each route’s components into a separate chunk, and only load them when the route is visited.
Combining Vue’s async component feature and Webpack’s code splitting feature, it’s trivially easy to lazy-load route components.
All we need to do is define our route components as async components:
const Foo = resolve => {
// require.ensure is Webpack's special syntax for a code-split point.
require.ensure(['./Foo.vue'], () => {
resolve(require('./Foo.vue'))
})
}
There’s also an alternative code-split syntax using AMD style require, so this can be simplified to:
const Foo = resolve => require(['./Foo.vue'], resolve)
Grouping Components in the Same Chunk
Sometimes we may want to group all the components nested under the same route into the same async chunk. To achieve that we need to use named chunks by providing a chunk name to require.ensure as the 3rd argument:
const Foo = r => require.ensure([], () => r(require('./Foo.vue')), 'group-foo')
const Bar = r => require.ensure([], () => r(require('./Bar.vue')), 'group-foo')
const Baz = r => require.ensure([], () => r(require('./Baz.vue')), 'group-foo')
the effect: if you render the component that you visited , this will have more .js files,but not included in vue app.js
as the photo show , there will have 2.js and 3.js , if not lazy load only have app.js
if you grouping your component in same chunk will the group chunk will have same .js file

通过结合Vue的异步组件特性和Webpack的代码分割特性,可以轻松实现路由组件的懒加载,从而减少初始页面加载时间。本文将详细介绍如何配置异步组件以实现按需加载,并展示如何将多个组件分组到同一个异步块中。
381

被折叠的 条评论
为什么被折叠?



