【随手记】uniapp + V3 使用TailwindCss3

目录


weapp-tailwindcss文档链接


一、CLI创建uniapp

bash 复制代码
npx degit dcloudio/uni-preset-vue#vite-ts my-vue3-project
npm install
npm run dev:h5

二、 安装TailwindCss3

bash 复制代码
npm install tailwindcss@3 postcss autoprefixer -D

三、初始化

  • 初始化TailwindCss、postcss,生成tailwind.config.jspostcss.config.js
bash 复制代码
npx tailwindcss init -p

tailwind.config.js

javascript 复制代码
/** @type {import('tailwindcss').Config} */
module.exports = {
  // 配置需要扫描的文件类型--即src下的所有vue、js、ts文件
  content: ['./public/index.html', './src/**/*.{html,js,ts,vue}'],
  // 自定义拓展配置
  theme: {
    // 覆盖默认配置
    colors: {
      'blue': '#3c9cff',
      'purple': '#7e5bef',
      'pink': '#ff49db',
      'orange': '#ff7849',
      'green': '#41B883',
      'yellow': '#ffc82c',
      'gray-dark': '#273444',
      'gray': '#8492a6',
      'gray-light': '#d3dce6',
    },
    // 新增额外配置
    extend: {
      // 扩展颜色
      colors: {
        'purple-pink': '#8135f5',
        'blue-light': '#9acafc'
      },
    },
  },
  // 配置插件
  plugins: [
    // 示例:自定义按钮样式:btn-primary <button class="btn-primary">点击我</button>
    function({ addComponents }) {
      addComponents({
        '.btn-primary': {
          '@apply bg-blue text-[#fff] font-bold rounded': '',
          '&:hover': {
            '@apply bg-blue-light' : '',
          }
        }
      })
    },
  ],
  // ...
  corePlugins: {
    // 小程序不需要 preflight,因为这主要是给 h5 的,如果你要同时开发小程序和 h5 端,你应该使用环境变量来控制它
    preflight: false
  }
}

postcss.config.js

javascript 复制代码
export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

四、引用TailwindCss

uniappApp.vue文件:

css 复制代码
<style>
  @tailwind base;
  @tailwind components;
  @tailwind utilities;
</style>

五、安装weapp-tailwindcss

bash 复制代码
npm install weapp-tailwindcss -D

注意:如果tailwindcssweapp-tailwindcss 之后安装,可以手动执行一下 patch 方法,npx weapp-tw patch

package.json文件:

javascript 复制代码
"scripts": {
	// 用于rpx单位支持和上下文通信
   "postinstall": "weapp-tw patch"
 }

六、注册插件

vite.config.ts文件:

typescript 复制代码
import { defineConfig } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import { UnifiedViteWeappTailwindcssPlugin as uvwt } from 'weapp-tailwindcss/vite';

export default defineConfig({
  // uni 是 uni-app 官方插件, uvtw 一定要放在 uni 后,对生成文件进行处理
  plugins: [uni(),uvwt()],
  css: {
    postcss: {
      plugins: [
        // require('tailwindcss')() 和 require('tailwindcss') 等价的,表示什么参数都不传,如果你想传入参数
        // require('tailwindcss')({} <- 这个是postcss插件参数)
        require('tailwindcss'),
        require('autoprefixer')
      ],
    },
  },
});

七、使用TailwindCss

javascript 复制代码
<template>
  <view class="flex flex-col items-center justify-center h-screen">
    <image alt="Vue logo" src="@/static/logo.png" class="w-[200rpx] h-[200rpx]" />
    <view class="font-[600] text-[40rpx] my-3 flex items-center flex-wrap justify-center">
      <text class="text-green">vue 3</text>+ 
      <text class="text-blue">vite</text>+ 
      <text class="text-orange">typescript</text> +
      <text class="text-purple-pink">tailwindcss4</text>
    </view>
    <button class="btn-primary">点击我</button>
  </view>
</template>

相关推荐
喵喵虫10 小时前
uniapp修改封装组件失败 styleIsolation
uni-app
人良爱编程10 小时前
Hugo的Stack主题配置记录03-背景虚化-导航栏-Apache ECharts创建地图
前端·javascript·apache·echarts·css3·html5
游戏开发爱好者821 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
Cult Of1 天前
Alicea Wind的个人网站开发日志(2)
开发语言·python·vue
宠友信息1 天前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”1 天前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
Byron07072 天前
从多端割裂到体验统一:基于 Vue 生态的跨端架构落地实战
vue·多端
计算机程序设计小李同学2 天前
基于 Spring Boot + Vue 的龙虾专营店管理系统的设计与实现
java·spring boot·后端·spring·vue