詳解VUE單頁應(yīng)用骨架屏方案
什么是骨架屏?
簡單的說,骨架屏就是在頁面未渲染完成的時候,先用一些簡單的圖形大致勾勒出頁面的基本輪廓,給用戶造成頁面正在加載的錯覺,待頁面渲染完成之后再用頁面替換掉骨架屏,從而減少頁面白屏的時間,給用戶帶來更好的體驗。

分析VUE渲染過程
使用vue-cli3.0創(chuàng)建項目: vue create project

在生成的項目文件夾下的public文件夾下的index.html文件代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico" rel="external nofollow" > <title>project</title> </head> <body> <noscript> <strong>We're sorry but project doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
可以看到,DOM里面有一個div#app,當(dāng)js被執(zhí)行完成之后,此div#app會被整個替換掉,因此,如何在Vue頁面實現(xiàn)骨架屏,已經(jīng)有了一個很清晰的思路——在div#app內(nèi)直接插入骨架屏相關(guān)內(nèi)容即可。
實現(xiàn)方案
手動在div#app里面寫入骨架屏內(nèi)容是不科學(xué)的,因此需要一個擴展性強且自動化的易維護方案。既然是在Vue項目里,所以所謂的骨架屏也是一個.vue文件,它能夠在構(gòu)建時由工具自動注入到div#app里面。 首先,我們在/src目錄下新建一個Skeleton.vue文件,其內(nèi)容如下:
<template>
<div class="skeleton page">
<div class="skeleton-nav"></div>
<div class="skeleton-swiper">
<div class="skeleton-swiper-item item-one"></div>
<div class="skeleton-swiper-item item-two"></div>
</div>
</div>
</template>
<style>
html,body,div{
margin:0;
padding:0;
}
.skeleton {
height: 100%;
overflow: hidden;
box-sizing: border-box;
background: #fff;
}
.skeleton-nav {
height: 54px;
background: #eee;
margin-bottom: 20px;
}
.skeleton-swiper {
min-height:600px;
max-width:1280px;
margin:0 auto;
}
.skeleton-swiper-item{
min-height: 600px;
height:100%;
background:#eee;
border-radius:5px;
}
.item-one{
width:20%;
float:left;
}
.item-two{
width:78%;
float:right;
}
</style>
接下來,在/src目錄再新建一個skeleton.entry.js入口文件:
import Vue from 'vue';
import Skeleton from './Skeleton.vue';
export default new Vue({
components: {
Skeleton,
},
template: '<skeleton />',
});
在完成了骨架屏的準(zhǔn)備之后,我們需要一個關(guān)鍵插件vue-server-renderer。該插件本用于服務(wù)端渲染,但是在這里,我們主要利用它能夠把.vue文件處理成html和css字符串的功能,來完成骨架屏的注入。
骨架屏注入
首先在public文件夾下新建一個template.html文件,并且其代碼和index.html文件代碼相同,但是需要在div#app中添加 <!--vue-ssr-outlet--> 占位符:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="./favicon.ico" rel="external nofollow" > <title>醫(yī)生工作臺</title> </head> <body> <noscript> <strong>We're sorry but yz_doctors doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"><!--vue-ssr-outlet--></div> <!-- built files will be auto injected --> </body> </html>
然后,我們還需要在根目錄新建一個webpack.skeleton.conf.js文件,以專門用來進行骨架屏的構(gòu)建。
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
module.exports = {
target: 'node',
entry: {
skeleton: './src/skeleton.entry.js',
},
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: '[name].js',
libraryTarget: 'commonjs2',
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader',
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
},
],
},
externals: nodeExternals({
whitelist: /\.css$/,
}),
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
},
extensions: ['*', '.js', '.vue', '.json'],
},
plugins: [
new VueLoaderPlugin(),
new VueSSRServerPlugin({
filename: 'skeleton.json',
}),
],
};
可以看到,該配置文件和普通的配置文件基本完全一致,主要的區(qū)別在于其target: 'node',配置了externals,以及在plugins里面加入了VueSSRServerPlugin。在VueSSRServerPlugin中,指定了其輸出的json文件名。然后通過運行下列指令,在/dist目錄下生成一個skeleton.json文件: webpack --config ./webpack.skeleton.conf.js 接下來,在根目錄下新建一個skeleton.js,該文件即將被用于往index.html內(nèi)插入骨架屏:
const fs = require('fs');
const { resolve } = require('path');
const htmlMinifier = require('html-minifier');
const createBundleRenderer = require('vue-server-renderer').createBundleRenderer;
// 先把vue的模板文件index.html置換成標(biāo)準(zhǔn)的模板,防止骨架屏污染
let tempData = fs.readFileSync(resolve(__dirname, './public/template.html'), 'utf-8');
fs.writeFileSync('./public/index.html', tempData, 'utf-8');
console.log('模板注入完成');
// 讀取`skeleton.json`,以`index.html`為模板寫入內(nèi)容
const renderer = createBundleRenderer(resolve(__dirname, './dist/skeleton.json'), {
template: fs.readFileSync(resolve(__dirname, './public/index.html'), 'utf-8'),
});
// 把上一步模板完成的內(nèi)容寫入(替換)`index.html`
renderer.renderToString({}, (err, html) => {
if (err) {
console.log(err);
return;
}
html = htmlMinifier.minify(html, {
collapseWhitespace: true,
minifyCSS: true,
});
fs.writeFileSync('./public/index.html', html, 'utf-8');
});
console.log('骨架屏注入完成');
接下來,只要運行 node skeleton.js ,就可以完成骨架屏的注入。 為了在 npm run serve 的時候自動完成骨架屏的注入,避免運行多次命令,需要在 package.json 中增加一條命令 "preserve": "webpack --config ./webpack.skeleton.conf.js && node skeleton.js" ,放在 "serve" 命令之前。
總結(jié)
新建template.html文件的目的是為了保存模板文件的干凈,因為每次完成骨架屏的注入后index.html文件中的 <!--vue-ssr-outlet--> 占位符已經(jīng)被骨架屏代碼所替換,再次修改骨架屏后就無法完成骨架屏的注入啦,所以在注入骨架屏?xí)r先用template.html文件中的內(nèi)容替換index.html文件,避免了每次修改骨架屏?xí)r還要手動修改index.html文件,運行一條命令實現(xiàn)骨架屏的自動注入。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中rem與postcss-pxtorem的應(yīng)用詳解
這篇文章主要介紹了Vue中rem與postcss-pxtorem的應(yīng)用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue中axios處理http發(fā)送請求的示例(Post和get)
本篇文章主要介紹了vue中axios處理http請求的示例(Post和get),這里整理了詳細的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
基于Vue中點擊組件外關(guān)閉組件的實現(xiàn)方法
下面小編就為大家分享一篇基于Vue中點擊組件外關(guān)閉組件的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

