reactjs Tailwind CSS在Vite-React-Typescript项目中不起作用

oaxa6hgo  于 2023-03-12  发布在  React
关注(0)|答案(1)|浏览(184)

我有这段代码,我想用它来测试是否一切都安装好了,这样我就可以开始一个新项目:

const App = () => {

  return (
    <div className="min-h-screen flex justify-center items-center">
      <h1 className="text-3xl font-bold text-blue-600">
        Install & Setup Vite + React + Typescript + Tailwind CSS 3
      </h1>
    </div>
  )
}

export default App

当我开始使用Vite时,我看到的是:

以下是我在package.json中安装的依赖项:

"scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@types/react": "^18.0.27",
    "@types/react-dom": "^18.0.10",
    "@vitejs/plugin-react": "^3.1.0",
    "autoprefixer": "^10.4.14",
    "postcss": "^8.4.21",
    "tailwindcss": "^3.2.7",
    "typescript": "^4.9.3",
    "vite": "^4.1.0"
  }

我的tailwind.config.cjs是:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./src/**/*.{js, jsx, ts, tsx}"
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

我的index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

我的main.tsx

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
)

过了一会儿,我在终端上看到了这个:
warn -在源文件中没有检测到实用程序类。如果这是意外情况,请在Tailwind CSS配置中仔细检查content选项。
警告-https://tailwindcss.com/docs/content-configuration
为什么我的设置不起作用?

cdmah0mi

cdmah0mi1#

您需要将index.html添加到tailwind.config.js中的内容数组中,并键入{js,ts,jsx,tsx},其中不带任何空格:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./index.html", // <= add this
    "./src/**/*.{js,ts,jsx,tsx}", // <= no spaces
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

相关问题