新建一个react+vite+tailwindui项目

  1. 创建 Vite 项目
    使用 Vite 创建一个新的 React 项目:

npm create vite@latest my-react-app --template react

my-react-app 是你的项目名称,你可以根据需要更改。

  1. 进入项目目录
    进入你刚刚创建的项目目录:

cd my-react-app

  1. 安装 Tailwind CSS
    在项目中安装 Tailwind CSS 及其依赖项:

npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p

  1. 配置 Tailwind CSS
    在 tailwind.config.js 文件中,配置 Tailwind 以包含所有模板文件:

/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./index.html", "./src/**/*.{js,ts,jsx,tsx}", ], theme: { extend: {}, }, plugins: [], }

  1. 添加 Tailwind CSS 到项目
    在 src/index.css 文件中,添加 Tailwind CSS 的基本样式:

@tailwind base; @tailwind components; @tailwind utilities;

  1. 启动开发服务器
    现在你可以启动开发服务器并查看你的项目:

npm run dev

  1. 创建一个简单的 React 组件
    在 src 目录下创建一个新的组件文件,例如 src/components/Button.jsx,并添加以下代码:

`import React from 'react';

const Button = ({ children }) => {
return (

);
};

export default Button;`

  1. 在 App.jsx 中使用组件
    在 src/App.jsx 中导入并使用你刚刚创建的 Button 组件:

`import React from 'react';
import Button from './components/Button';

function App() {
return (




);
}

export default App;`

  1. 查看结果
    现在,打开浏览器并访问 http://localhost:3000,你应该会看到一个带有 Tailwind CSS 样式的按钮。

总结
通过以上步骤,你已经成功创建了一个使用 React、Vite 和 Tailwind CSS 的项目。你可以继续扩展这个项目,添加更多的组件和样式。

posted @ 2024-11-06 14:28  小陈皮皮  阅读(134)  评论(0)    收藏  举报