1.路由懶載入 大專案中的路由懶載入不適用於開發環境(會導致儲存程式碼時熱更新程式碼時間很長20s左右),我們需要配置不同環境下的路由配置,目錄如下

在 router/ 資料夾中加入2個資料夾:
_import_development.js
module.exports = file => require('@/' + file + '.vue').default
複製程式碼
_import_production.js
module.exports = file => resolve => require(['@/' + file + '.vue'], resolve)
複製程式碼
index.js
const IMPORT = require('./_import_' + process.env.NODE_ENV + '.js');
複製程式碼
元件引入時寫入:
component: IMPORT('pages/mainPage') // 元件
複製程式碼
這樣在開發環境時編譯程式碼時間會大大加快,在生產環境也會使用到懶載入的路由,減小首屏載入的體積,看下效果


18.7s → 4.35s 大概優化了76%的時間。