vue3、react和taro/react(h5和小程序)配置tailwindCSS

7 篇文章 0 订阅
6 篇文章 0 订阅

难点:不太会

知识点:在vite中配置tailwindCSS,tailwindcss中文文档
1.先说vue3和react的,先安装依赖,我喜欢用pnpm,各位随意

pnpm install -D tailwindcss postcss autoprefixer

2.生成相关文件

npx tailwindcss init -p
// 会生成2个文件
// tailwind.config.js
/** @type {import('tailwindcss').Config} */
export default {
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
}

// postcss.config.js
export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

3.修改tailwind.config.js中的content

/** @type {import('tailwindcss').Config} */
export default {
  content: ["./index.html",'./src/**/*.{vue,js,ts,jsx,tsx}'],
  theme: {
    extend: {},
  },
  plugins: [],
}

4.在main.js中引入css,这里可以单独创建一个css去引入(taro中是app.js)

import "tailwindcss/tailwind.css"

5.测试一下,加了一个白底的圆角背景(taro中换成View)

<p class="p-3 mx-auto text-[#3a5fa3] text-center rounded-3xl bg-white my-3 text-2xl">
    孩子档案
</p>

6.有n多玩法,比如给样式增加一个 !important和弄个全局主题色,可以自己琢磨,有好的玩法也可以推荐给大家

/** @type {import('tailwindcss').Config} */
export default {
  content: ["./index.html",'./src/**/*.{vue,js,ts,jsx,tsx}'],
  theme: {
    extend: {},
  },
  important: true,
  plugins: [],
  theme: {
    extend: {
      colors: {
        "primary-color": "var(--primary-color)",
        "secondary-color": "var(--secondary-color)"
      },
    },
  },
}

7.说下在taro/react中怎么配置,H5和以上的通用,微信小程序在上面基础上,找到config/index.js文件添加下面配置

由于我项目只有两端,只举例了微信小程序和H5的配置,其他端自行配置

pnpm install -D weapp-tailwindcss
// 微信小程序
const { UnifiedWebpackPluginV5 } = require('weapp-tailwindcss/webpack');

const config = {
  // 你的其他配置...
  mini: {
    // 你的其他配置...
    webpackChain(chain, webpack) {
      chain.merge({
        plugin: {
          install: {
            plugin: UnifiedWebpackPluginV5,
            args: [{
              appType: 'taro'
            }]
          }
        }
      });
      chain.performance.hints(false); // 关闭taro中的性能提示
    },
  },
}

8.顺便说说uni接入tailwindCSS一直失败,索性就不用了(**想吐槽几句),换成unocss它不香吗?这个也兼容tailwindCSS的class,有兴趣的可以看我写的其他端接入unocss,大家可以多提提意见

hbx/uni3/vite/微信小程序 接入unocss

react 接入 unocss 

微信原生小程序接入 unocss

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咸喵小宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值