編碼的世界 / 優質文選 / 生涯

vue3.0+vite 使用 postcss-pxtorem 實現移動自適應(px轉rem)


2022年3月20日
-   

  • 下載 postcss-pxtorem

npm i postcss-pxtorem -D
  • package.json同級目錄創建postcss.config.js文件

module.exports = {
plugins: {
autoprefixer: {
overrideBrowserslist: [
"Android 4.1",
"iOS 7.1",
"Chrome > 31",
"ff > 31",
"ie >= 8",
"last 10 versions", // 所有主流瀏覽器最近10版本用
],
grid: true,
},
'postcss-pxtorem': {
rootValue: 37.5,
propList: ['*'],
unitPrecision: 5
}
}
}

這裏只實現了 pxrem,還要安裝 amfe-flexible
npm i amfe-flexible -D
main.ts文件中 import 一下就好可以了
import ‘amfe-flexible/index.js’
:可能會出現下面情況 [vite] Internal server error: Loading PostCSS Plugin failed: Cannot find module 'autoprefixer' 這時候嘗試安裝 autoprefixer就可以了
npm i autoprefixer

熱門文章