Vite

安装并配置 Vite.

创建项目

先通过 vite 新建一个 Vue 项目:

pnpm create vite@latest my-vue-app --template vue-ts

添加 Tailwind CSS

pnpm add tailwindcss @tailwindcss/vite

src/style.css 中的内容替换为以下内容:

src/style.css
css
@import "tailwindcss";

编辑 tsconfig.json 文件

tsconfig.jsontsconfig.app.json 文件的 compilerOptions 部分添加 baseUrlpaths 属性:

ts
{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.node.json"
    }
  ],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

编辑 tsconfig.app.json 文件

为 IDE 解析路径,将以下代码添加到 tsconfig.app.json

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

更新 vite.config.ts

添加以下代码到 vite.config.ts,以便您的应用能在不出错的情况下解析路径:

pnpm add -D @types/node

typescript
import path from 'node:path'
import tailwindcss from '@tailwindcss/vite'
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [vue(), tailwindcss()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
})

运行 CLI

运行 shadcn-vue init 命令来设置您的项目:

pnpm dlx shadcn-vue@latest init

您将被提示回答一些问题以配置 components.json

txt
Which color would you like to use as base color? › Neutral

添加组件

您现在可以开始为项目添加组件。

pnpm dlx shadcn-vue@latest add button

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

vue
<script setup lang="ts">
import { Button } from '@/components/ui/button'
</script>

<template>
  <div>
    <Button>点击我</Button>
  </div>
</template>
Edit this page on GitHub