跳到正文
语言22
菜单

文档 / 框架

Astro

把 UI Loft 项目添加到 Astro 网站。Astro 需要先添加 React 和 Tailwind,而且会动的项目需要 client 指令。

版本
React
需要
React + Tailwind 4 + shadcn
设置时间
约 5 分钟

设置

  1. 创建网站

    如果已经有了就跳过。

    终端
    npm create astro@latest my-site -- --template minimal --install --no-git --yes
    cd my-site
  2. 添加 React 和 Tailwind

    Astro 自带的命令会同时装好两者并更新 astro.config.mjs,还会生成 src/styles/global.css。

    终端
    npx astro add react tailwind --yes
  3. 添加 @ 别名

    在 tsconfig.json 的 compilerOptions 下加上 paths。

    tsconfig.json
    {
      "compilerOptions": {
        "paths": {
          "@/*": ["./src/*"]
        }
      }
    }
  4. 设置 shadcn

    它会生成 components.json,并把主题写入 src/styles/global.css。-d 采用默认设置。

    终端
    npx shadcn@latest init -d
  5. 添加一个项目

    从任意项目页面复制命令。文件会放进 src/components/。

    终端
    npx shadcn@latest add https://uiloft.dev/r/glow-button.json
  6. 使用它

    导入一次样式和项目,再给项目加上 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,不必在每个页面里导入。

其他框架