詳解VUE單頁(yè)應(yīng)用骨架屏方案
什么是骨架屏?
簡(jiǎn)單的說(shuō),骨架屏就是在頁(yè)面未渲染完成的時(shí)候,先用一些簡(jiǎn)單的圖形大致勾勒出頁(yè)面的基本輪廓,給用戶造成頁(yè)面正在加載的錯(cuò)覺(jué),待頁(yè)面渲染完成之后再用頁(yè)面替換掉骨架屏,從而減少頁(yè)面白屏的時(shí)間,給用戶帶來(lái)更好的體驗(yàn)。
分析VUE渲染過(guò)程
使用vue-cli3.0創(chuàng)建項(xiàng)目: vue create project
在生成的項(xiàng)目文件夾下的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里面有一個(gè)div#app,當(dāng)js被執(zhí)行完成之后,此div#app會(huì)被整個(gè)替換掉,因此,如何在Vue頁(yè)面實(shí)現(xiàn)骨架屏,已經(jīng)有了一個(gè)很清晰的思路——在div#app內(nèi)直接插入骨架屏相關(guān)內(nèi)容即可。
實(shí)現(xiàn)方案
手動(dòng)在div#app里面寫(xiě)入骨架屏內(nèi)容是不科學(xué)的,因此需要一個(gè)擴(kuò)展性強(qiáng)且自動(dòng)化的易維護(hù)方案。既然是在Vue項(xiàng)目里,所以所謂的骨架屏也是一個(gè).vue文件,它能夠在構(gòu)建時(shí)由工具自動(dòng)注入到div#app里面。 首先,我們?cè)?src目錄下新建一個(gè)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>
接下來(lái),在/src目錄再新建一個(gè)skeleton.entry.js入口文件:
import Vue from 'vue'; import Skeleton from './Skeleton.vue'; export default new Vue({ components: { Skeleton, }, template: '<skeleton />', });
在完成了骨架屏的準(zhǔn)備之后,我們需要一個(gè)關(guān)鍵插件vue-server-renderer。該插件本用于服務(wù)端渲染,但是在這里,我們主要利用它能夠把.vue文件處理成html和css字符串的功能,來(lái)完成骨架屏的注入。
骨架屏注入
首先在public文件夾下新建一個(gè)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ī)生工作臺(tái)</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>
然后,我們還需要在根目錄新建一個(gè)webpack.skeleton.conf.js文件,以專門(mén)用來(lái)進(jìn)行骨架屏的構(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文件名。然后通過(guò)運(yùn)行下列指令,在/dist目錄下生成一個(gè)skeleton.json文件: webpack --config ./webpack.skeleton.conf.js
接下來(lái),在根目錄下新建一個(gè)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`為模板寫(xiě)入內(nèi)容 const renderer = createBundleRenderer(resolve(__dirname, './dist/skeleton.json'), { template: fs.readFileSync(resolve(__dirname, './public/index.html'), 'utf-8'), }); // 把上一步模板完成的內(nèi)容寫(xiě)入(替換)`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('骨架屏注入完成');
接下來(lái),只要運(yùn)行 node skeleton.js
,就可以完成骨架屏的注入。 為了在 npm run serve
的時(shí)候自動(dòng)完成骨架屏的注入,避免運(yùn)行多次命令,需要在 package.json
中增加一條命令 "preserve": "webpack --config ./webpack.skeleton.conf.js && node skeleton.js"
,放在 "serve"
命令之前。
總結(jié)
新建template.html文件的目的是為了保存模板文件的干凈,因?yàn)槊看瓮瓿晒羌芷恋淖⑷牒骾ndex.html文件中的 <!--vue-ssr-outlet-->
占位符已經(jīng)被骨架屏代碼所替換,再次修改骨架屏后就無(wú)法完成骨架屏的注入啦,所以在注入骨架屏?xí)r先用template.html文件中的內(nèi)容替換index.html文件,避免了每次修改骨架屏?xí)r還要手動(dòng)修改index.html文件,運(yùn)行一條命令實(shí)現(xiàn)骨架屏的自動(dòng)注入。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue項(xiàng)目中keepAlive的使用說(shuō)明(超級(jí)實(shí)用版)
這篇文章主要介紹了Vue項(xiàng)目中keepAlive的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換
這篇文章主要為大家詳細(xì)介紹了vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Vue中rem與postcss-pxtorem的應(yīng)用詳解
這篇文章主要介紹了Vue中rem與postcss-pxtorem的應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Vue 源碼分析之 Observer實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了 Vue 源碼分析之 Observer實(shí)現(xiàn)過(guò)程,Observer 最主要的作用就是實(shí)現(xiàn)了touch -Data(getter) - Collect as Dependency這段過(guò)程,也就是依賴收集的過(guò)程,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-03-03vue中axios處理http發(fā)送請(qǐng)求的示例(Post和get)
本篇文章主要介紹了vue中axios處理http請(qǐng)求的示例(Post和get),這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Vue.extend實(shí)現(xiàn)掛載到實(shí)例上的方法
這篇文章主要介紹了Vue.extend實(shí)現(xiàn)掛載到實(shí)例上的方法,結(jié)合實(shí)例形式分析了Vue.extend實(shí)現(xiàn)掛載到實(shí)例上的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05基于Vue中點(diǎn)擊組件外關(guān)閉組件的實(shí)現(xiàn)方法
下面小編就為大家分享一篇基于Vue中點(diǎn)擊組件外關(guān)閉組件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03