99.1k

Astro

上一页下一页

在 Astro 中安装并配置 shadcn/ui。

创建项目

先创建一个新的 Astro 项目:

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

编辑 tsconfig.json

tsconfig.json 中加入以下配置以解析路径别名:

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

运行 CLI

执行 shadcninit 命令完成项目初始化:

pnpm dlx shadcn@latest init

添加组件

现在可以向项目中添加组件。

pnpm dlx shadcn@latest add button

上述命令会加入 Button 组件,你可以按如下方式导入:

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 className="grid place-items-center h-screen content-center">
			<Button>Button</Button>
		</div>
	</body>
</html>