懒加载也叫延迟加载,即在需要的时候进行加载,随用随载
在单页应用中,如果没有应用懒加载,运用webpack打包后的文件将会异常的大,
造成进入首页时,需要加载的内容过多,延时过长,不利于用户体验, 运用懒加载可以将页面进行划分,按需加载页面,可以分担首页所承担的加载压力,减少加载用时。一种代码分块的语法,使用 AMD 风格的 require
const Foo = resolve => require(['./Foo.vue'], resolve);const router = new VueRouter({ routes: [ { path: '/foo', component: Foo } ]})
//router/index.jsimport Vue from 'vue'import Router from 'vue-router'Vue.use(Router)const router = new Router({ routes: [ { mode: 'history', path: '/home', name: 'home', component: resolve => require([URL], resolve),//懒加载 children: [ { mode: 'history', path: '/home/:name', name: 'any', component: resolve => require(['@/components/any'], resolve),//懒加载 }, ] }, { mode: 'history', path: '/my', name: 'my', component: resolve => require(['@/components/my'], resolve),//懒加载, children: [ { mode: 'history', path: '/my/:name', name: 'any', component: resolve => require(['@/components/any'], resolve),//懒加载 }, ] }, { path: '/login', name: 'Login', component: resolve=>require(['@/components/login'],resolve) }, ]});