vue-cli3.0 使用px2rem 或 postcss-plugin-px2rem

px2rem

  1. 構建項目(vue-cli3.0)css

    vue create hello-world (Manuall select features)前端

  2. 安裝px2rem-loader(devDependencies)vue

    npm install px2rem-loader --save-devnode

  3. 移動端適配解決npm包 "lib-flexible" (dependencies)正則表達式

    npm install lib-flexible --savevue-cli

  4. main.js中引入 "lib-flexible"npm

    import 'lib-flexible' // 移動端適配 (目錄: hello-world/src/main.js)json

  5. 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
             })
             <!--新增結束-->
         }
     }
     
    複製代碼
    1. login.vue中根據750px設計圖寫頁面
    <style scoped>
            .login-wrap {
                background: #666666;
                height: 100px;
            }
        </style>
        
        iphone6: height: 1.3333rem
    複製代碼

--此時是已經成功了,可是px2rem-loader這裏只能僅限於css,並不能知足大多數開發需求,好比使用less,stylus,scss...這個時候就須要postcss--

postcss-plugin-px2rem

---在上個例子基礎上---bash

  1. 安裝 "postcss-plugin-px2rem" (devDependencies)

    npm install postcss-plugin-px2rem --save-dev

  2. 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
                            }),
                        ]
                    }
                }
            }
            <!--新增結束-->
        }
複製代碼
  1. package.json 中加入postcss 相關插件
{
            "dependencies": { .. }
            "postcss": {
                "plugins": {
                    "autoprefixer": {},
                    "precss": {}
                }
            }
        }
複製代碼
  1. 在login.vue中
<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
複製代碼

此時就能夠在項目中成功使用less,scss,styuls和px2rem了


若是有錯誤請指出,謝謝

相關文章
相關標籤/搜索