vue-skeleton-webpack-plugin骨架屏

vue-skeleton-webpack-plugin骨架屏使用

插件github地址

安裝插件
npm install vue-skeleton-webpack-plugin
在項目中創建骨架屏展示組件

平時項目中使用的是rem適配,然後發現骨架屏中無效,因爲他出現的時候並未渲染頁面,因此找不到window對象,獲取不到屏寬

<template>
  <div>
    <div class="skeleton">
      <div class="skeleton-head"></div>
      <div class="skeleton-body">
        <div class="skeleton-name"></div>
        <div class="skeleton-title"></div>
        <div class="skeleton-content"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'skeleton'
};
</script>

<style scoped>
.skeleton {
  padding: 15px;
}
.skeleton .skeleton-head,
.skeleton .skeleton-name,
.skeleton .skeleton-title,
.skeleton .skeleton-content,
.skeleton .skeleton-content {
  background: rgb(194, 207, 214);
}
.skeleton-head {
  width: 33px;
  height: 33px;
  border-radius: 50%;
  float: left;
}
.skeleton-body {
  margin-left: 50px;
}
.skeleton-name{
  width: 150px;
  height: 30px;
  margin-bottom: 10px;
}
.skeleton-title {
  width: 100%;
  height: 30px;
}
.skeleton-content {
  width: 100%;
  height: 30px;
  margin-top: 10px;
}
</style>
創建骨架屏入口文件entry-skeleton.js
import Vue from 'vue'
import Skeleton from './Skeleton'
export default new Vue({
  components: {
    Skeleton
  },
  template: `
    <div>
      <skeleton id="skeleton1" style="display:none"/>
    </div>`
})
在build目錄下創建webpack.skeleton.conf.js
'use strict';
const path = require('path') 
const merge = require('webpack-merge') 
const baseWebpackConfig = require('./webpack.base.conf') 
const nodeExternals = require('webpack-node-externals')
const config = require('../config')
const utils = require('./utils')
const isProduction = process.env.NODE_ENV === 'production'
const sourceMapEnabled = isProduction
  ? config.build.productionSourceMap
  : config.dev.cssSourceMap

function resolve(dir) {
  return path.join(__dirname, dir)
}

let skeletonWebpackConfig = merge(baseWebpackConfig, {
  target: 'node',
  devtool: false,
  entry: {
    app: resolve('../src/entry-skeleton.js')
  },
  output: Object.assign({}, baseWebpackConfig.output, {
    libraryTarget: 'commonjs2'
  }),
  externals: nodeExternals({
    whitelist: /\.css$/
  }),
  plugins: []
})

// important: enable extract-text-webpack-plugin 
// 重點配置
skeletonWebpackConfig.module.rules[0].options.loaders = utils.cssLoaders({
  sourceMap: sourceMapEnabled,
  extract: true
})

module.exports = skeletonWebpackConfig

如果是vue-cli腳手架創建項目,一定要注意是否開啓樣式分離
官方描述
修改方案就是在webpack.skeleton.conf.js中設置如下

skeletonWebpackConfig.module.rules[0].options.loaders = utils.cssLoaders({
  sourceMap: sourceMapEnabled,
  extract: true
})
接下來在webpack.prod.conf.js和webpack.dev.conf.js plugins中引入插件
// inject skeleton content(DOM & CSS) into HTML
    new SkeletonWebpackPlugin({
      webpackConfig: require('./webpack.skeleton.conf'),
      quiet: true,
      minimize: true,
      router: {
        mode: 'history',
        routes: [
          {
            path: '/client/a/Quiksns/comment',    //對應使用路由
            skeletonId: 'skeleton1'    // 所用骨架屏的id標識
          },
        ]
      }
    }),

在這裏插入圖片描述

因爲我這是第一次使用這個骨架屏,所以我讓他展示多個的方法就是寫了多個內容,如下(可能有更好,更方便的,後續修改的時候在改一下)
在這裏插入圖片描述
在頁面加載初期實現的效果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章