您好,登錄后才能下訂單哦!
本篇內容主要講解“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搭建移動端”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。