px2rem-loader(Vue:将px转化为rem,适配移动端)
- 使用的是vue-cli+webpack,通过npm来安装的
npm i lib-flexible --save - 在main.js中引入lib-flexible
import ‘lib-flexible/flexible’ - 通过meta标签,设置设备宽度以及缩放比例
meta name="viewport" content="width=device-width, initial- scale=1.0"
- 安装px2rem-loader
npm install px2rem-loader - 配置px2rem-loader
在build文件中找到util.js,将px2rem-loader添加到cssLoaders中,如:
const cssLoader = {
loader: 'css-loader',
options: {
sourceMap: options.sourceMap
}
}
const postcssLoader = {
loader: 'postcss-loader',
options: {
sourceMap: options.sourceMap
}
}
const px2remLoader = {
loader: 'px2rem-loader',
options: {
remUnit: 75 // 设计稿是750px高
}
}
同时,在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)
}
}