1.路由懒加载 大项目中的路由懒加载不适用于开发环境(会致使保存代码时热更新代码时间很长20s左右),咱们须要配置不一样环境下的路由配置,目录以下vue
在 router/ 文件夹中加入2个文件夹:优化
_import_development.jsui
module.exports = file => require('@/' + file + '.vue').default
复制代码
_import_production.jsspa
module.exports = file => resolve => require(['@/' + file + '.vue'], resolve)
复制代码
index.jscode
const IMPORT = require('./_import_' + process.env.NODE_ENV + '.js');
复制代码
组件引入时写入:component
component: IMPORT('pages/mainPage') // 组件
复制代码
这样在开发环境时编译代码时间会大大加快,在生产环境也会使用到懒加载的路由,减少首屏加载的体积,看下效果router
18.7s → 4.35s 大概优化了76%的时间。cdn