您好,登錄后才能下訂單哦!
這篇文章主要講解了“React怎么引入antd-mobile+postcss搭建移動端”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“React怎么引入antd-mobile+postcss搭建移動端”吧!
安裝antd-mobile
npm install antd-mobile --save
在App.js
引入css
import 'antd-mobile/dist/antd-mobile.css';
在jsx
使用antd組件
import React from 'react'; import { Button } from 'antd-mobile'; const index = () => { return ( <div> <Button type="primary">primary</Button> </div> ); } export default index;
npm install babel-plugin-import -s
安裝插件,覆蓋webpack
配置
customize-cra配置api文檔
npm install react-app-rewired customize-cra -s
package.json
進行更改命令方式
"scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-app-rewired eject" },
在根目錄新建config-overrides.js
const { override, fixBabelImports } = require('customize-cra'); module.exports = override( fixBabelImports('import', { libraryName: 'antd-mobile', style: 'css', }), );
在App.js
刪除之前引入的css
校驗是否按需引入
npm install lib-flexible postcss-px2rem-exclude --save
index.js
導入
import 'lib-flexible'
修改config-overrides.js
文件
const { override, fixBabelImports, addPostcssPlugins, addWebpackAlias} = require('customize-cra'); const path = require("path"); module.exports = override( fixBabelImports('import', { libraryName: 'antd-mobile', style: 'css', }), addPostcssPlugins( [require("postcss-px2rem-exclude") ( { remUnit: 75, //設計圖大小 remPrecision: 2, //只轉換到兩位小數 exclude: /node_modules/i //插件不需要轉rem } ) ] ), addWebpackAlias({ "@": path.resolve(__dirname, "src") }) );
如果你需要使用less
/
自定義主題
npm install less less-loader -s
如果項目啟動報錯,是因為less-loader
版本過高,需要卸載,安裝低版本
npm install less-loader@5.0.0 -s
坑! 需要注意順序
const { override, fixBabelImports, addPostcssPlugins, addWebpackAlias, addLessLoader } = require('customize-cra'); const path = require("path"); module.exports = override( fixBabelImports('import', { libraryName: 'antd-mobile', style: true, //默認為'css' }), addLessLoader({ javascriptEnabled: true, modifyVars: { "@brand-primary": "#1DA57A" }, //自定義主題 }), addPostcssPlugins( [require("postcss-px2rem-exclude") ( { remUnit: 75, //設計圖大小 remPrecision: 2, //只轉換到兩位小數 exclude: /node_modules/i //插件不需要轉rem } ) ] ), addWebpackAlias({ "@": path.resolve(__dirname, "src") }) );
補充:解決react項目引入antd-mobile導致postcss配置px轉rem失效問題
今天使用了一下antd-mobile發現我之前配置的postcss失效了,防止下次踩坑記錄一下解決方案
解決方案:在config-overrides.js文件里重寫postcss,加入如下代碼
npm下載以下模塊
npm i react-app-rewire-postcss postcss-px2rem-exclude -S
const { override, fixBabelImports, addWebpackAlias, addDecoratorsLegacy, } = require("customize-cra"); const path = require("path"); const rewirePostcss = require("react-app-rewire-postcss"); module.exports = override( // 配置按需加載 fixBabelImports("import", { libraryName: "antd-mobile", style: "css", }), // 配置文件別名 addWebpackAlias({ "@": path.resolve(__dirname, "src"), "@scss": path.resolve(__dirname, "src/assets/scss"), "@images": path.resolve(__dirname, "src/assets/images"), "@views": path.resolve(__dirname, "src/views"), "@network": path.resolve(__dirname, "src/network"), "@store": path.resolve(__dirname, "src/store"), "@components": path.resolve(__dirname, "src/components"), }), addDecoratorsLegacy(), (config, env) => { // 重寫postcss rewirePostcss(config, { plugins: () => [ require("postcss-flexbugs-fixes"), require("postcss-preset-env")({ autoprefixer: { flexbox: "no-2009", }, stage: 3, }), require("postcss-px2rem-exclude")({ // 設計稿寬度/10 remUnit: 1080 / 10, exclude: /node-modules/i, }), ], }); return config; } );
感謝各位的閱讀,以上就是“React怎么引入antd-mobile+postcss搭建移動端”的內容了,經過本文的學習后,相信大家對React怎么引入antd-mobile+postcss搭建移動端這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。