1. 程式人生 > >vue-skeleton-webpack-plugin骨架屏

vue-skeleton-webpack-plugin骨架屏

comment padding === try 獲取 pan 適配 rod plugin

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

插件github地址:https://github.com/lavas-project/vue-skeleton-webpack-plugin

安裝插件
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標識
          },
        ]
      }
    }),

技術分享圖片

因為我這是第一次使用這個骨架屏,所以我讓他展示多個的方法就是寫了多個內容,如下(可能有更好,更方便的,後續修改的時候在改一下)

技術分享圖片

在頁面加載初期實現的效果

技術分享圖片

個人csdn博客:https://blog.csdn.net/zmkyf1993

vue-skeleton-webpack-plugin骨架屏