您的位置:首页 > 产品设计 > UI/UE

Vue Lazy Loading Routes

2017-03-18 19:12 274 查看
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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: