安装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
配置
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', 遇见作文800字 style: true, //默认为'css' }), addlessloader({ javascriptenabled: true, modifyvars: { "@brand-primary": "#1da57a" }, //自定义主题 }), addpostcssplugins( [require("postcss-px2rem-exclude") ( { 一年级国庆手抄报 remunit: 75, //设计图大小 rempr太空港ecision: 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, addweb箭牌衬衫packalias, 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/asts/scss"), "@images": path.resolve(__dirname, "src/asts/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-pret-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搭建移动端的资料请关注www.887551.com其它相关文章!
本文发布于:2023-04-03 21:06:29,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/566bcb29a55a14a4f276e5fd45a9305f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:React引入antd.doc
本文 PDF 下载地址:React引入antd.pdf
留言与评论(共有 0 条评论) |