首页 > web前端 > js教程 > 正文

Vue + Tailwind 和动态类

碧海醫心
发布: 2024-10-08 18:45:10
转载
1179人浏览过

vue + tailwind 和动态类

我最近在做的一个项目使用了 vite、vue 和 tailwind。

使用自定义颜色一段时间后,我遇到了一些困惑。

在模板中添加和使用自定义颜色不是问题 - 使用 tailwind 文档使该过程非常清晰

// tailwind.config.js
module.exports = {
    theme: {
        colors: {
          'custom-green': {
              50: '#9bd1b2',
              ...
              700: '#284735'
          },
        }
    }
}
登录后复制

我的问题是在 vue 模板中使用带有动态和静态 css 类的自定义颜色时。

使用 npm run dev 或 vite 运行项目时,bg-custom-green-50 或 text-custom-green-50 不起作用,并且从未出现在 css 文件中。

立即学习前端免费学习笔记(深入)”;

我的理解是,如果你的完整 css 类名不存在于模板中,那么 tailwind 不会添加它或在 css 文件中生成它。

萌动AI
萌动AI

CreateAI旗下AI动漫视频生成平台

萌动AI 438
查看详情 萌动AI

假设 css 类:text-custom-green-50 或 bg-custom-green-50 未在项目中的其他任何地方使用

下面的例子将不起作用

<template>
    <h3 :class="['font-bold', colorclass]">{{ heading }}</h3>
</template>
<script type="text/javascript">
    const colorclass = ref('')

    // color being set somewhere else in the component logic
    colorclass.value = 'text-custom-green-50'
</script>
登录后复制

下面的例子将起作用

<template>
    <h3 :class="['font-bold', colorclass]">{{ heading }}</h3>
    <p class="text-custom-green">green text</p>
</template>
<script type="text/javascript">
    const colorclass = ref('')

    // color being set somewhere else in the component logic
    colorclass.value = 'text-custom-green-50'
</script>
登录后复制

两个示例之间的区别是 text-custom-green css 类添加到模板中,因此 tailwind 会将其添加到生成的 css 文件中。

要克服这个问题,您可以将任何自定义颜色或 tailwind 类添加到 tailwind.config.js 文件中的安全列表中。

// tailwind.config.js
module.exports = {
    safelist: [
        'text-custom-green-50',
        'bg-custom-green-50'
    ]
}
登录后复制

即使这些颜色没有直接在模板中使用,而是在另一点动态添加,它们也将可用

希望其他人觉得这有帮助。

以上就是Vue + Tailwind 和动态类的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号