如何用尾风在NUXT3中动态更改类别?
我是Nuxt3和tailwind的新手,我正在研究一个动态呈现4个细分市场的部分。
<section class="howItWorks">
<div class="clientchoice__desc">
<h4 class="mb-2">{{item.title}}</h4>
<p class="mb-4">{{item.desc}}</p>
<h2 class="mt-44 number">{{item.number}}</h2>
</div>
</section>
动态渲染的数据来自这里:
export default {
data() {
return {
howItWorks: [
{
icon: '/svgs/line.png',
title: 'Title',
desc: 'Description here',
number: '1'
},
{
icon: '/svgs/line.png',
title: 'Title',
desc: 'Description here',
number: '2'
},
{
icon: '/svgs/line.png',
title: 'Title',
desc: 'Description here',
number: '3'
},
{
icon: '/svgs/line.png',
title: 'Title',
desc: 'Description here',
number: '4'
},
],
}
}
}
我想对描述下出现的每个数字(1,2,3和4)具有不同的样式。例如具有不同的颜色等
。使CSS类动态变化的最佳方法是什么?
谢谢。
I'm very new to Nuxt3 and Tailwind, and I'm working on a section which dynamically renders 4 segments.
<section class="howItWorks">
<div class="clientchoice__desc">
<h4 class="mb-2">{{item.title}}</h4>
<p class="mb-4">{{item.desc}}</p>
<h2 class="mt-44 number">{{item.number}}</h2>
</div>
</section>
The dynamically rendered data comes from here:
export default {
data() {
return {
howItWorks: [
{
icon: '/svgs/line.png',
title: 'Title',
desc: 'Description here',
number: '1'
},
{
icon: '/svgs/line.png',
title: 'Title',
desc: 'Description here',
number: '2'
},
{
icon: '/svgs/line.png',
title: 'Title',
desc: 'Description here',
number: '3'
},
{
icon: '/svgs/line.png',
title: 'Title',
desc: 'Description here',
number: '4'
},
],
}
}
}
I would like to have different styling for the each of the numbers (1,2,3 and 4) that appear under the descriptions. Such as having different colors etc.
Is it possible? What would be the best way to have the css class change dynamically?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们可以将一个对象传递到:类(v-bind:class的简短)到动态切换类
的语法意味着活动类的存在将取决于数据属性的真实性。
ISACTIVE可以是计算的属性,甚至可以是一种方法,只要它返回真实值
活动
类,将设置为元素。从Vue官方 doc doc << /a>对于您的情况,您可以在
howitworks
数组上运行v-for循环,并且每个项目都使用计算属性或方法将特定类返回基于项目的动态呈现。下面给出了一个可能的演示代码,即如何在多种方式中动态添加类。To see how this below code's outcome go to this vue playground link
We can pass an object to :class (short for v-bind:class) to dynamically toggle classes
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive.
isActive can be a computed property or even a method, as long as it's return a truthy value the
active
class will be set to the element. Learn other ways of dynamic class rendering from vue official docFor your case, you can run a v-for loop on
howItWorks
array and for each item use a computed property or method to return a specific class to dynamiclly render based on the item. Below given one possible demo code of how can you dynamically add class among many ways.To see how this below code's outcome go to this vue playground link
您可以将V-Bind:class =“”与class =“”一起使用。
这意味着您可以创建一个变量并在需要时更改它:
You can use v-bind:class="" alongside class="".
This means that you can create a variable and change it when you need to: