【随手记】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>

相关推荐
andr_gale2 天前
02_uniapp自定义上凸效果的底部TabBar
前端·javascript·uni-app·移动端
倾听醉梦语2 天前
React/Vite/Next.js 前端开发工具 SpotPatch:点击页面元素精准定位 JSX/TSX 源码
javascript·react·ai编程·vite·next.js·前端开发工具
不爱说话郭德纲2 天前
uni-app x iOS 扫码模糊怎么办?AVFoundation 灰尘级别摄像头助你超广角聚焦
前端·uni-app·app
CHB2 天前
uni-app x 蒸汽模式 性能测试基准报告 Benchmark【Android版】
android·ios·uni-app
DsirNg3 天前
从 `@wheel.prevent.stop` 到事件捕获:一次讲清浏览器事件流与 Vue 事件修饰符
javascript·vue·事件修饰符·事件冒泡·dom 事件·事件捕获·wheel
其美杰布-富贵-李3 天前
Vue 3 模板语法速通:彻底理解 `:`、`@`、`#`、`v-if`、`v-for` 与 `v-model`
vue
DsirNg3 天前
基于拓扑排序的画布自动布局:用最长上游路径决定节点列级
vue·流程图·拓扑排序·状态管理·有向图·自动布局·最长路径
游戏开发爱好者84 天前
系统状态:专为iPhone和iPad设计的实时系统性能监控工具全面介绍
android·ios·小程序·uni-app·iphone·webview·ipad
2501_915909064 天前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
way_hj5 天前
Flask笔记(2)快速web项目
笔记·flask·vue·web