Docs
Astro

Astro

为 Astro 安装并配置 shadcn/ui

创建项目

¥Create project

首先创建一个新的 Astro 项目:

¥Start by creating a new Astro project:

pnpm create astro@latest astro-app  --template with-tailwindcss --install --add react --git

编辑 tsconfig.json 文件

¥Edit tsconfig.json file

将以下代码添加到 tsconfig.json 文件中以解析路径:

¥Add the following code to the tsconfig.json file to resolve paths:

tsconfig.json
{
  "compilerOptions": {
    // ...
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
    // ...
  }
}

运行 CLI

¥Run the CLI

运行 shadcn init 命令来设置你的项目:

¥Run the shadcn init command to setup your project:

pnpm dlx shadcn@latest init

添加组件

¥Add Components

你现在可以开始向你的项目添加组件。

¥You can now start adding components to your project.

pnpm dlx shadcn@latest add button

上面的命令将把 Button 组件添加到你的项目中。然后你可以像这样导入它:

¥The command above will add the Button component to your project. You can then import it like this:

src/pages/index.astro
---
import { Button } from "@/components/ui/button"
---
 
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width" />
		<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
		<meta name="generator" content={Astro.generator} />
		<title>Astro + TailwindCSS</title>
	</head>
 
	<body>
		<div class="grid place-items-center h-screen content-center">
			<Button>Button</Button>
		</div>
	</body>
</html>