文档 / 框架
Astro
把 UI Loft 项目添加到 Astro 网站。Astro 需要先添加 React 和 Tailwind,而且会动的项目需要 client 指令。
- 版本
- React
- 需要
- React + Tailwind 4 + shadcn
- 设置时间
- 约 5 分钟
设置
创建网站
如果已经有了就跳过。
终端npm create astro@latest my-site -- --template minimal --install --no-git --yes cd my-site添加 React 和 Tailwind
Astro 自带的命令会同时装好两者并更新
astro.config.mjs,还会生成src/styles/global.css。终端npx astro add react tailwind --yes添加 @ 别名
在
tsconfig.json的compilerOptions下加上paths。tsconfig.json{ "compilerOptions": { "paths": { "@/*": ["./src/*"] } } }设置 shadcn
它会生成
components.json,并把主题写入src/styles/global.css。-d采用默认设置。终端npx shadcn@latest init -d添加一个项目
从任意项目页面复制命令。文件会放进
src/components/。终端npx shadcn@latest add https://uiloft.dev/r/glow-button.json使用它
导入一次样式和项目,再给项目加上
client:load,让它在浏览器中运行。src/pages/index.astro--- import "@/styles/global.css" import { GlowButton } from "@/components/glow-button" --- <html lang="en"> <body> <GlowButton client:load>Get started</GlowButton> </body> </html>
须知
- 没有 client 指令时,Astro 会把项目当作纯 HTML 发送:能显示,但不会动,也不会响应操作。
- 用
client:visible代替client:load,会等项目滚动到可见时才加载。 - 如果有布局,请在布局里导入一次
@/styles/global.css,不必在每个页面里导入。