構建項目(vue-cli3.0)css
vue create hello-world (Manuall select features)
前端
安裝px2rem-loader(devDependencies)vue
npm install px2rem-loader --save-dev
node
移動端適配解決npm包 "lib-flexible" (dependencies)正則表達式
npm install lib-flexible --save
vue-cli
main.js中引入 "lib-flexible"npm
import 'lib-flexible' // 移動端適配 (目錄: hello-world/src/main.js)
json
Create new "vue.config.js" file if without "vue.config.js" (目錄: hello-world/vue.config.js)數組
module.exports = {
chainWebpack: (config) => {
<!--新增的內容-->
config.module
.rule('css')
.test(/\.css$/)
.oneOf('vue')
.resourceQuery(/\?vue/)
.use('px2rem')
.loader('px2rem-loader')
.options({
remUnit: 75
})
<!--新增結束-->
}
}
複製代碼
<style scoped>
.login-wrap {
background: #666666;
height: 100px;
}
</style>
iphone6: height: 1.3333rem
複製代碼
---在上個例子基礎上---bash
安裝 "postcss-plugin-px2rem" (devDependencies)
npm install postcss-plugin-px2rem --save-dev
vue.config.js 配置 postcss-plugin-px2rem
module.exports = {
lintOnSave: true,
<!--新增的內容-->
css: {
loaderOptions: {
postcss: {
plugins: [
require('postcss-plugin-px2rem')({
rootValue: 75, //換算基數, 默認100 ,這樣的話把根標籤的字體規定爲1rem爲50px,這樣就能夠從設計稿上量出多少個px直接在代碼中寫多上px了。
// unitPrecision: 5, //容許REM單位增加到的十進制數字。
//propWhiteList: [], //默認值是一個空數組,這意味着禁用白名單並啓用全部屬性。
// propBlackList: [], //黑名單
exclude: /(node_module)/, //默認false,能夠(reg)利用正則表達式排除某些文件夾的方法,例如/(node_module)\/若是想把前端UI框架內的px也轉換成rem,請把此屬性設爲默認值
// selectorBlackList: [], //要忽略並保留爲px的選擇器
// ignoreIdentifier: false, //(boolean/string)忽略單個屬性的方法,啓用ignoreidentifier後,replace將自動設置爲true。
// replace: true, // (布爾值)替換包含REM的規則,而不是添加回退。
mediaQuery: false, //(布爾值)容許在媒體查詢中轉換px。
minPixelValue: 3 //設置要替換的最小像素值(3px會被轉rem)。 默認 0
}),
]
}
}
}
<!--新增結束-->
}
複製代碼
{
"dependencies": { .. }
"postcss": {
"plugins": {
"autoprefixer": {},
"precss": {}
}
}
}
複製代碼
<style lang="scss" scoped>
.login-wrap {
background: #666666;
height: 100px;
.text-span {
font-size: 16px;
color: red;
}
}
</style>
複製代碼
iphone6:
height: 100px; => height: 1.3333rem
font-size: 16px; => font-size: 0.21333rem
複製代碼
若是有錯誤請指出,謝謝