骨架屏能够理解为是当数据还未加载进来前,页面的一个空白版本,一个简单的关键渲染路径。用户会看到一个样式简单,描绘了当前页面的大体框架的骨架屏页面,而后骨架屏中各个占位部分被实际资源彻底替换,这个过程当中用户会以为内容正在逐渐加载即将呈现,下降了用户的焦躁情绪,使得加载过程主观上变得流畅。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