骨架屏能夠理解爲是當數據還未加載進來前,頁面的一個空白版本,一個簡單的關鍵渲染路徑。用戶會看到一個樣式簡單,描繪了當前頁面的大體框架的骨架屏頁面,而後骨架屏中各個佔位部分被實際資源徹底替換,這個過程當中用戶會以爲內容正在逐漸加載即將呈現,下降了用戶的焦躁情緒,使得加載過程主觀上變得流暢。css
這裏採用餓了麼開源的方案page-skeleton-webpack-plugin。html
npm install --save-dev page-skeleton-webpack-plugin npm install --save-dev html-webpack-plugin
安裝過程當中報錯提示以下vue
ERROR: Failed to download Chromium r515411! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOA D" env variable to skip download.
執行這個命令webpack
npm config set puppeteer_download_host=https://storage.googleapis.com.cnpmjs.org
我這個項目是基於vue-cli3
腳手架開發的。git
建立一個vue.config.js
文件。github
const { SkeletonPlugin } = require('page-skeleton-webpack-plugin') const path = require('path') module.exports = { configureWebpack: { plugins: [ new SkeletonPlugin({ pathname: path.resolve(__dirname, './shell'), // 用來存儲 shell 文件的地址 staticDir: path.resolve(__dirname, './dist'), // 最好和 `output.path` 相同 routes: ['/'], // 將須要生成骨架屏的路由添加到數組中 excludes: ['.van-nav-bar', '.van-tabbar'] // 須要忽略的css選擇器 }) ], }, chainWebpack: (config) => { // 解決vue-cli3腳手架建立的項目壓縮html 幹掉<!-- shell -->致使骨架屏不生效 if (process.env.NODE_ENV !== 'development') { config.plugin('html').tap(opts => { opts[0].minify.removeComments = false return opts }) } }, };
在項目根目錄下面建立一個shell
文件夾。
chainWebpack配置 這個是解決vue-cli3
打包的骨架屏不生效的BUGweb
在你啓動 App 的根元素內部添加 <!-- shell -->vue-cli
<body> <div id="app"><!-- shell --></div> <!-- built files will be auto injected --> </body>
https://github.com/lanpangzhinpm
https://github.com/cnpm/cnpmjs.org/issues/1246
https://github.com/ElemeFE/page-skeleton-webpack-plugin