文章标题

  • 01 环境准备
  • 02 快速构建
    • 2.1 参数选项
    • 2.2 umi@x 还是 @umijs/max
    • 2.3 使用 pnpm (推荐)
    • 2.4 使用 npm 和 yarn
    • 2.5 启动项目
    • 2.6 启用 Prettier(可选)
    • 2.7 打包部署发布
  • 03 Tailwind CSS 插件(可选)
    • 3.1 安装配置
    • 3.2 可能遇到的问题
  • 04 React 中的 classnames
    • 4.1 安装
    • 4.2 使用
      • 参考示例如下
      • 在React中使用

01 环境准备

首先得有 node,并确保 node 版本是 18 或以上

  • windows 下推荐用 nvm 来管理 node 版本(nvm中文文档)
  • mac 下推荐使用 n 来管理 node 版本(npm-n 官方文档)

安装 node 👇

# windows: nvm 操作
$ nvm install 18.20.0
$ nvm use 18.20.0
$ node -v
v18.20.0# mac: n 操作
$ sudo n 18.20.0
$ node -v
v18.20.0

还需要包管理工具,node 默认包含 npm,但也可以选择其他方案,如:pnpmyarn

此处选择安装 pnpm👇

# 方式1: 一个普通版本,它需要Node.js来运行。
npm install -g pnpm# 方式2: 与Node.js一起打包成一个可执行文件,所以它可以在没有安装Node.js的系统上使用。
npm install -g @pnpm/exe

02 快速构建

2.1 参数选项

使用 create-umi 创建项目时,可用的参数如下:

option(可选参数)描述
–no-git创建项目,但不初始化 Git
–no-install创建项目,但不自动安装依赖

2.2 umi@x 还是 @umijs/max

使用 create-umi 选择 Ant Design Pro 模板,就能使用 @umijs/max 来创建项目了

$ npx create-umi@latest
? Pick Umi App Template › - Use arrow-keys. Return to submit.Simple App
❯   Ant Design ProVue Simple App

Umi Max 项目中命令行请使用 max{:bash},而不是原来的 umi{:bash},示例如下 👇

$ npx max g jest

历史项目升级:如果使用了 @umijs/max 可以使用 max 命令来替换 umi,如 max dev,max build 等。而 umi@4 将一些项目前置操作放到了 setup 命令中,如 umi@3 中的 umi g tmp 等命令,需要使用 umi setup 替换,示例如下 👇

// package.json
{"scripts": {
-    "build": "umi build",
+    "build": "max build",
-    "postinstall": "umi g tmp",
+    "postinstall": "max setup",
-    "start": "umi dev",
+    "start": "max dev",}
}

详细升级步骤参考 升级到 Umi 介绍

2.3 使用 pnpm (推荐)

国内建议选 pnpm + taobao 源,速度提升明显。这一步会自动安装依赖,同时安装成功后会自动执行 umi setup 做一些文件预处理等工作。

$ pnpm dlx create-umi@latest
✔ Install the following package: create-umi? (Y/n) · true
┌   create-umi 
│
○  What's the target folder name?
│  my-app
│
○  Pick Umi App Template
│  ● Simple App 
│  ○ Ant Design Pro  
│  ○ Vue Simple App
│  ○ Umi Plugin
│
○  Pick Npm Client
│  ○ npm
│  ○ cnpm
│  ○ tnpm
│  ○ yarn
│  ● pnpm (recommended)
│
○  Pick Npm Registry
│  ○ npm 
│  ● taobao
│
└  You're all set!...Done in 1m 45.8s using pnpm v10.10.0

2.4 使用 npm 和 yarn

# npm
$ npx create-umi@latest
Need to install the following packages:create-umi@latest
Ok to proceed? (y) y
...# yarn 
yarn create umi
success Installed "create-umi@4.0.6" with binaries:- create-umi
...

2.5 启动项目

执行 pnpm dev 命令,在浏览器里打开 http://localhost:8000/,就能看到项目界面

$ pnpm dev
...╔════════════════════════════════════════════════════╗║ App listening at:                                  ║║  >   Local: http://localhost:8000                  ║
ready - ║  > Network: http://192.168.11.188:8000             ║║                                                    ║║ Now you can open browser with the above addresses↑ ║╚════════════════════════════════════════════════════╝
...        

2.6 启用 Prettier(可选)

如果需要用 prettier 做项目代码的自动格式化,执行 pnpm umi g 命令

$ pnpm umi g
✔ Pick generator type › Enable Prettier -- Enable Prettier
info  - Write package.json
info  - Write .prettierrc
info  - Write .prettierignore
info  - Install dependencies with pnpm

2.7 打包部署发布

执行 pnpm build 命令

$ pnpm build
event - compiled successfully in 1179 ms (567 modules)
event - build index.html

打包文件默认会生成到 ./dist 目录下

./dist
├── index.html
├── umi.css
└── umi.js

完成构建后,就可以把 dist 目录部署到服务器上了。

03 Tailwind CSS 插件(可选)

3.1 安装配置

Umi 和 Umi Max 项目使用 Tailwind CSS 功能,使用微生成器一键开启该插件

Umi 项目

$ npx umi g tailwindcss
info  - Update package.json for devDependencies
set config:tailwindcss on /project/max-playground/.umirc.ts
info  - Update .umirc.ts
info  - Write tailwind.config.js
info  - Write tailwind.css

Umi Max 项目

$ npx max g tailwindcss
info  - Update package.json for devDependencies
set config:tailwindcss on /project/max-playground/.umirc.ts
info  - Update .umirc.ts
info  - Write tailwind.config.js
info  - Write tailwind.css

至此就可以在项目中使用 Tailwind CSS 的样式,并且可以在项目根目录的 tailwind.config.jstailwind.css 根据需要修改配置。

注意需要同步插件依赖 👇

$ pnpm i

在项目根目录添加 .env 文件,添加 CHECK_TIMEOUT 变量,用于设置 Tailwind CSS 插件的检查间隔时间。

# Default: 5
CHECK_TIMEOUT=10

3.2 可能遇到的问题

(1)终端报错 Unexpected unknown at-rule “@tailwind” at-rule-no-unknown
问题描述:因为 @umijs/max 项目已经添加 husky,并为项目生成 precommit 配置(即 git commit message 格式校验行为),在每次 git commit 前会将 Git 暂存区的代码默认格式化。 但使用 vscode 用户,在 commit 代码时可能会遇到下面这样的问题:

tailwind.css1:1  ✖  Unexpected unknown at-rule "@tailwind"  at-rule-no-unknown2:1  ✖  Unexpected unknown at-rule "@tailwind"  at-rule-no-unknown3:1  ✖  Unexpected unknown at-rule "@tailwind"  at-rule-no-unknownhusky - pre-commit script failed (code 1)

问题原因:vscode 编辑器可能识别不了 “@tailwind” 导致 pre-commit 没有通过
解决方案1:找到 .husky 下的 pre-commit 文件进行删除
解决方案2:在 commit 时加上 --no-verify 参数绕过检查

git commit -m "xxx" --no-verify

解决方案3: https://github.com/tailwindlabs/tailwindcss/discussions/5258
1.在项目的根目录下创建 .vscode,并用两个名为 settings.jsontailwind.json
2.更新 settings.jsontailwind.json文件

// settings.json
{"css.customData": [".vscode/tailwind.json"],// (识别您正在使用的文件类型,例如css)"css.lint.unknownAtRules": "ignore"
}// tailwind.json
{"version": 1.1,"atDirectives": [{"name": "@tailwind","description": "Use the `@tailwind` directive to insert Tailwind's `base`, `components`, `utilities` and `screens` styles into your CSS.","references": [{"name": "Tailwind Documentation","url": "https://tailwindcss.com/docs/functions-and-directives#tailwind"}]},{"name": "@apply","description": "Use the `@apply` directive to inline any existing utility classes into your own custom CSS. This is useful when you find a common utility pattern in your HTML that you’d like to extract to a new component.","references": [{"name": "Tailwind Documentation","url": "https://tailwindcss.com/docs/functions-and-directives#apply"}]},{"name": "@responsive","description": "You can generate responsive variants of your own classes by wrapping their definitions in the `@responsive` directive:\n```css\n@responsive {\n  .alert {\n    background-color: #E53E3E;\n  }\n}\n```\n","references": [{"name": "Tailwind Documentation","url": "https://tailwindcss.com/docs/functions-and-directives#responsive"}]},{"name": "@screen","description": "The `@screen` directive allows you to create media queries that reference your breakpoints by **name** instead of duplicating their values in your own CSS:\n```css\n@screen sm {\n  /* ... */\n}\n```\n…gets transformed into this:\n```css\n@media (min-width: 640px) {\n  /* ... */\n}\n```\n","references": [{"name": "Tailwind Documentation","url": "https://tailwindcss.com/docs/functions-and-directives#screen"}]},{"name": "@variants","description": "Generate `hover`, `focus`, `active` and other **variants** of your own utilities by wrapping their definitions in the `@variants` directive:\n```css\n@variants hover, focus {\n   .btn-brand {\n    background-color: #3182CE;\n  }\n}\n```\n","references": [{"name": "Tailwind Documentation","url": "https://tailwindcss.com/docs/functions-and-directives#variants"}]}]
}

04 React 中的 classnames

classnames 是一个强大的工具,可以帮助你在 React 项目中更优雅地处理 className。通过它,你可以轻松地根据条件动态生成 className,从而使代码更加简洁和易读。它还支持混合对象、数组和字符串参数,使得支持可选的 className 属性更加简单。通过使用 classnames,你可以避免手动拼接字符串,从而减少错误并提高代码的可维护性。

4.1 安装

$ npm install classnames
# 或者
$ yarn add classnames
# 或者
$ pnpm i classnames

4.2 使用

参考示例如下

// classNames函数接受任意数量的参数,这些参数可以是字符串或对象
classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
// 数组也可以按照上述规则递归平展:
const arr = ['b', { c: true, d: false }];
classNames('a', arr); // => 'a b c'

在React中使用

import React, { useState } from 'react';
import classNames from 'classnames';export default function Test() {return (<><div className={classNames('foo', 'bar')}>classNames基础使用实例</div><div className={classNames('foo', 'bar')}><span> classNames+TailwindCss 使用实例</span><span  className={classNames('title', '!text-red-600')}>单独为特定原子化CSS增加"!"前辍让它生成的原子化CSS添加"!important"后辍来提高样式优先级</span></div>        </>
);
}

参考 Umijs官网

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/86232.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/86232.shtml
英文地址,请注明出处:http://en.pswp.cn/pingmian/86232.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JDK 17 中 java.lang.System 常用方法及应用场景

概述 java.lang.System 在 JDK 17 中依然是最核心的系统交互类之一。以下是针对 JDK 17 的常用方法详解&#xff0c;包含新特性和最佳实践。 一、标准 I/O 流&#xff08;更新至 JDK 17&#xff09; 1. 控制台输出 // 传统输出方式&#xff08;仍然可用&#xff09; System…

深入探究Manticoresearch Java API:新增与查询文档实战

引言Java 项目集成 Manticoresearch新增文档操作查询文档操作 SQL 查询API 查询 总结 引言 Manticore Search 是一个使用 C 开发的高性能搜索引擎&#xff0c;创建于 2017 年&#xff0c;其前身是 Sphinx Search。它显著改进了 Sphinx 的功能&#xff0c;修复了数百个错误&am…

Linux远程机器无法连接-------解决方案

笔者这几天碰到linux机器远程ssh 无法连接的情况 背景分析 笔者在linux机器进行测试的时候&#xff0c;偶发突然无法连接机器&#xff0c;如下图所示&#xff0c;vscode的远程连接也无法进行。 telnet也无法登录。 解决方案 笔者的登录场景是这样的&#xff0c;所以怀疑是…

网络模型中,应用层支持什么协议

在网络模型中&#xff08;无论是 OSI 七层模型 还是 TCP/IP 四层模型&#xff09;&#xff0c;应用层 是最顶层&#xff0c;直接为用户或应用程序提供网络服务接口。它支持的是各种具体的应用程序协议&#xff0c;这些协议定义了特定类型应用程序如何进行通信和数据交换。 以下…

比特币序数理论与铭文的终极指南

引言&#xff1a;比特币网络的意外趋势 去年&#xff0c;比特币网络上出现了一个意外的趋势——这一趋势令许多比特币纯粹主义者感到愤怒和惊讶&#xff0c;但同时也在更广泛的加密货币社区中点燃了对这个行业最古老、最安全区块链的希望和热情。 这个趋势涉及铭文&#xff0…

C/C++ DLL 动态链接库的开发与示例详解

简介 介绍 QT6&#xff0c;DLL 动态链接库的开发&#xff0c;开发示例 详细 DLL 入口函数 DllMain 是每个 dll 的入口函数&#xff0c;可以写&#xff0c;也可以不写&#xff0c;不影响其他库函数运行。如果使用此函数需要包含头文件#include <objbase.h>。 BOOL WI…

在 CentOS 7 上基于 Docker 的 LNMP 部署 Hexo

时间起因是因为之前BLOG没有备份数据都丢失了,今天在和朋友聊天的时候讨论到到底使用Hexo还是用Halo?最后还是想熟悉下这两种博客的架设和部署方式,每次部署都有各种不同的问题,旨在提升自己的学习认知。以此有机会得到更多大佬们的指导~ 因为我是在 CentOS 7 服务器上使用…

《Go小技巧易错点100例》第三十六篇

本期分享&#xff1a; 1.使用gops获取正在运行的Go进程 2.将静态文件编译到Go程序中 3.Go语言通过多重赋值实现变量值交换 使用gops获取正在运行的Go进程 在 Go 语言开发中&#xff0c;进程诊断和性能分析是保障服务稳定性的关键环节。Google 开源的 gops [https://github.…

Idea新UI

轻松上手Idea新UI&#xff1a;开启高效编程新体验 在软件开发领域&#xff0c;IntelliJ IDEA一直以其强大的功能和出色的性能深受开发者喜爱。而其推出的new ui&#xff0c;更是为我们带来了全新的操作体验&#xff0c;进一步提升了开发效率。今天&#xff0c;就来详细讲讲如何…

QML\QtQuick\QtWidgets适合的场景及其优缺点

在Qt框架中&#xff0c;QML、QtQuick和QtWidgets是三种不同的UI开发技术&#xff0c;各有其适用场景和优缺点。以下是它们的对比分析&#xff1a; 1. QtWidgets 适用场景&#xff1a; 传统的桌面应用程序&#xff08;Windows/macOS/Linux&#xff09;。 需要复杂控件&#xf…

Spring Boot 2.x 项目搭建 (二)

因为Spring Boot 2.x 项目搭建 &#xff08;一&#xff09;访问 start.spring.io 或通过IDE&#xff08;如IntelliJ IDEA&#xff09;的Spring Initializr向导创建项目时&#xff0c;只能使用jdk17以上的版本&#xff0c;这里我需要兼容老项目需要JDK1.8&#xff0c;所以进行一…

Python 前端框架/工具合集

前言 Python 本身并不是为前端开发而设计的语言&#xff0c;但确实有一些 Python 框架可以“变相”地用于前端开发&#xff0c;比如生成 HTML、控制前端页面逻辑&#xff0c;甚至可以直接运行在浏览器中。 目标 本文将对常见的使用python实现的前端框架&#xff0c;先做一个…

Swagger 在 Spring Boot 中的详细使用指南

Swagger 是一个强大的 API 文档生成工具&#xff0c;在 Spring Boot 项目中主要通过 springdoc-openapi 库实现。下面我将详细讲解 Swagger 的配置、注解使用和高级功能。 一、基础配置 1. 添加依赖 在 pom.xml 中添加&#xff1a; xml 复制 下载 运行 <dependency&…

群晖如何开启及使用ssh:小白用户上手指南-家庭云计算专家

最近很多用户希望提供群晖下onlyoffice的安装服务,但是很多都是萌新小白,不知道怎么启用ssh和使用ssh客户端.这里提供简单的方法,统一答复: 重要的事情说3遍:一定不要自己发挥,全部按说明的来;一定要在内网环境用内网ip访问;不要用域名和端口号,谢谢各位萌新了!!! 一、ssh启用…

Ubuntu查看与切换显示管理器(已测试ubuntu20,ubuntu24)

一级标题 显示管理器是用户使用图形化的登录界面。 当用户成功输入用户名和密码后&#xff0c;即可开始使用图形化界面会话。gdm3和 lightdm 都是显示管理器。 它们提供了图形化登录并处理用户身份验证。 查看正在使用的显示管理器&#xff1a; cat /etc/X11/default-displa…

ROS 2 中 Astra Pro 相机与 YOLOv5 检测功能编译启动全记录

先记录大概主要流程&#xff0c;后期有时间了再完善细节以及附上截图 一、编译 Astra Pro 相机功能包 1. 清理并编译相机包 cd ~/Astra_ws rm -rf build/astra_camera install/astra_camera # 清理旧编译 colcon build --packages-select astra_camera --parallel-workers …

当实体类名和数据库表列名不一致时不能封装数据的解决办法

1. <resultMap> 是什么&#xff1f; 作用&#xff1a;<resultMap> 用来定义数据库表字段和Java实体类属性之间的对应关系。 为什么需要&#xff1f; 数据库字段通常用下划线命名&#xff08;如 brand_name&#xff09;&#xff0c;而Java类属性用驼峰命名&#…

window系统上labelImg的安装与使用

一、window上安装Anaconda 下载&#xff1a;https://repo.anaconda.com/archive/选择版本&#xff0c;此次选择的是Anaconda3-2025.06-0-Windows-x86_64.exe双击安装在 Windows 搜索栏中输入 Anaconda Prompt 并打开。 二、安装pyton3.8 wondow环境 在 Windows 搜索栏中输入…

SSE 流与普通 HTTP 响应的区别

1. 连接特性 特性普通 HTTP 响应SSE 流连接持续时间一次性&#xff0c;请求-响应后立即关闭持久连接&#xff0c;保持打开状态通信方向单向 (客户端→服务器→客户端)单向 (服务器→客户端)数据格式完整响应体流式文本数据 (text/event-stream) 2. 数据传输方式 普通 HTTP 响…

Proteus仿真51单片机控制L298N PWM电机调速

51单片机概述 51单片机是指Intel公司开发的MCS-51系列单片机及其兼容产品&#xff0c;是应用最广泛的8位单片机系列之一。其名称来源于该系列的第一款芯片8051。 主要特点&#xff1a; 8位CPU&#xff1a;处理数据宽度为8位 哈佛结构&#xff1a;程序存储器和数据存储器分开…