其他分享
首页 > 其他分享> > px2rem-loader(Vue:将px转化为rem,适配移动端)

px2rem-loader(Vue:将px转化为rem,适配移动端)

作者:互联网



1:使用的是vue-cli+webpack,通过npm来安装的

npm i lib-flexible --save

2:在main.js中引入lib-flexible

import 'lib-flexible/flexible'

3:通过meta标签,设置设备宽度以及缩放比例
<meta name="viewport" content="width=device-width, initial-scale=1.0">

  4.安装px2rem-loader

npm install px2rem-loader

 5.配置px2rem-loader

 在build文件中找到util.js,将px2rem-loader添加到cssLoaders中,如:

const cssLoader = {
    loader: 'css-loader',
    options: {
      minimize: process.env.NODE_ENV === 'production',
      sourceMap: options.sourceMap
    }
  }
  const px2remLoader = {
    loader: 'px2rem-loader',
    options: {
      remUnit: 75
    }
  }
同时,在generateLoaders方法中添加px2remLoader

function generateLoaders (loader, loaderOptions) {
  const loaders = options.usePostCSS ? [cssLoader, postcssLoader, px2remLoader] : [cssLoader, px2remLoader]

if (loader) {
  loaders.push({
  loader: loader + '-loader',
  options: Object.assign({}, loaderOptions, {
  sourceMap: options.sourceMap
  })
  })
  }


if (options.extract) {
  return ExtractTextPlugin.extract({
  use: loaders,
  fallback: 'vue-style-loader'
  })
  } else {
  return ['vue-style-loader'].concat(loaders)
  }
}

 

标签:loaders,Vue,px2rem,适配,loader,sourceMap,px2remLoader,options
来源: https://www.cnblogs.com/luckyShuang/p/11233712.html