Docs
TanStack Start

TanStack Start

安装并配置 TanStack Start。

TanStack Start + Tailwind v4

创建项目

¥Create project

按照 TanStack Start 网站上的 从头开始构建项目 指南创建一个新的 TanStack Start 项目。

¥Start by creating a new TanStack Start project by following the Build a Project from Scratch guide on the TanStack Start website.

添加 Tailwind

¥Add Tailwind

安装 tailwindcss 及其依赖。

¥Install tailwindcss and its dependencies.

pnpm add tailwindcss @tailwindcss/postcss postcss

创建 postcss.config.ts

¥Create postcss.config.ts

在项目的根目录下创建一个 postcss.config.ts 文件。

¥Create a postcss.config.ts file at the root of your project.

postcss.config.ts
export default {
  plugins: {
    "@tailwindcss/postcss": {},
  },
}

创建 app/styles/app.css

¥Create app/styles/app.css

app/styles 目录中创建一个 app.css 文件并导入 tailwindcss

¥Create an app.css file in the app/styles directory and import tailwindcss

app/styles/app.css
@import "tailwindcss" source("../");

导入 app.css

¥Import app.css

app/routes/__root.tsx
import type { ReactNode } from "react"
import { Outlet, createRootRoute } from "@tanstack/react-router"
import { Meta, Scripts } from "@tanstack/start"
 
import appCss from "@/styles/app.css?url"
 
export const Route = createRootRoute({
  head: () => ({
    meta: [
      {
        charSet: "utf-8",
      },
      {
        name: "viewport",
        content: "width=device-width, initial-scale=1",
      },
      {
        title: "TanStack Start Starter",
      },
    ],
    links: [
      {
        rel: "stylesheet",
        href: appCss,
      },
    ],
  }),
  component: RootComponent,
})

编辑 tsconfig.json 文件

¥Edit tsconfig.json file

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

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

tsconfig.json
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "moduleResolution": "Bundler",
    "module": "ESNext",
    "target": "ES2022",
    "skipLibCheck": true,
    "strictNullChecks": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["./app/*"]
    }
  }
}

运行 CLI

¥Run the CLI

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

¥Run the shadcn init command to setup your project:

pnpm dlx shadcn@canary init -d

这将在项目的根目录中创建一个 components.json 文件并在 app/styles/app.css 中配置 CSS 变量。

¥This will create a components.json file in the root of your project and configure CSS variables inside app/styles/app.css.

就是这样

¥That's it

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

¥You can now start adding components to your project.

pnpm dlx shadcn@canary add button

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

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

app/routes/index.tsx
import { Button } from "@/components/ui/button"
 
function Home() {
  const router = useRouter()
  const state = Route.useLoaderData()
 
  return (
    <div>
      <Button>Click me</Button>
    </div>
  )
}

基本入门

¥Basic Starter

基本入门 模板已配置 Tailwind v3。

¥The Basic Starter template has Tailwind v3 already configured.

运行 CLI

¥Run the CLI

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

¥Run the shadcn init command to setup your project:

pnpm dlx shadcn@canary init -d

这将在项目的根目录中创建一个 components.json 文件并在 app/styles/app.css 中配置 CSS 变量。

¥This will create a components.json file in the root of your project and configure CSS variables inside app/styles/app.css.

就是这样

¥That's it

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

¥You can now start adding components to your project.

pnpm dlx shadcn@canary add button

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

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

app/routes/index.tsx
import { Button } from "@/components/ui/button"
 
function Home() {
  const router = useRouter()
  const state = Route.useLoaderData()
 
  return (
    <div>
      <Button>Click me</Button>
    </div>
  )
}