歡迎您光臨本站 註冊首頁

Vue CLI3移動端適配(px2rem或postcss-plugin-px2rem)_vue.js

←手機掃碼閱讀     火星人 @ 2020-04-27 , reply:0

我們使用Vue CLI3 做一個移動端適配 。

前言

首先確定你的項目是Vue CLI3版本以上的。

一、移動端適配包

1、安裝移動端適配包

npm i lib-flexible -S

2、在 main.js 引入適配包

import Vue from 'vue' import App from './App.vue' import 'lib-flexible' // 引入適配包 Vue.config.productionTip = false new Vue({ render: h => h(App), }).$mount('#app')

3、在 index.html 編輯 viewport



二、CSS樣式適配

1、安裝 px2rem-loader 包( 只適用於css樣式 )

npm i px2rem-loader -D

2、在 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 }) }, }

三、CSS預處理語言樣式適配

1、安裝 postcss-plugin-px2rem ( 適用於css預處理語言 )

npm i postcss-plugin-px2rem -D

2、在 vue.config.js 配置

module.exports={ 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 }), ] } } } }

3、在 package.json 配置,加入 postcss 相關插件

{ "name": "app", "version": "0.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "lint": "vue-cli-service lint" }, "dependencies": { "core-js": "^3.6.4", "lib-flexible": "^0.3.2", "vue": "^2.6.11" }, "devDependencies": { "@vue/cli-plugin-babel": "~4.3.0", "@vue/cli-plugin-eslint": "~4.3.0", "@vue/cli-service": "~4.3.0", "babel-eslint": "^10.1.0", "eslint": "^6.7.2", "eslint-plugin-vue": "^6.2.2", "node-sass": "^4.14.0", "postcss-plugin-px2rem": "^0.8.1", "px2rem-loader": "^0.1.9", "sass-loader": "^8.0.2", "vue-template-compiler": "^2.6.11" }, "postcss": { "plugins": { "autoprefixer": {}, "precss": {} } } }


[火星人 ] Vue CLI3移動端適配(px2rem或postcss-plugin-px2rem)_vue.js已經有365次圍觀

http://coctec.com/docs/vue-js/show-post-231805.html