vue-cli3.0 使用px2rem 或 postcss-plugin-px2rem
px2rem
-
構建項目(vue-cli3.0)javascript
vue create hello-world (Manuall select features)
css -
安裝px2rem-loader(devDependencies)前端
npm install px2rem-loader --save-dev
vue -
移動端適配解決npm包 "lib-flexible" (dependencies)java
npm install lib-flexible --save
node -
main.js中引入 "lib-flexible"正則表達式
import 'lib-flexible' // 移動端適配 (目錄: hello-world/src/main.js)
vue-cli -
Create new "vue.config.js" file if without "vue.config.js" (目錄: hello-world/vue.config.js)npm
module.exports = { chainWebpack: (config) => { <!--新增的內容--> config.module .rule('css') .test(/\.css$/) .oneOf('vue') .resourceQuery(/\?vue/) .use('px2rem') .loader('px2rem-loader') .options({ remUnit: 75 }) <!--新增結束--> } }
-
在.vue中根據750px設計圖寫頁面json
<style scoped> .wrap { background: #666666; height: 100px; } </style> iphone6: height: 1.3333rem
--此時是已經成功了,可是px2rem-loader這裏只能僅限於css,並不能知足大多數開發需求,好比使用less,stylus,scss...這個時候就須要postcss--
postcss-plugin-px2rem
---在上個例子基礎上---
-
安裝 "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 }), ] } } } <!--新增結束--> }
package.json 中加入postcss 相關插件
{ "dependencies": { .. } "postcss": { "plugins": { "autoprefixer": {}, "precss": {} } } }