[英]How to conditionally apply CSS class in a VueJs object iteration?
Code in Vuetify: Vuetify 中的代码:
<v-layout row wrap>
<v-flex xs2 v-for="(month, key) in months" :key ="key">
<router-link class = "decoration" :to="month.target">{{(month.month)}}</router-link>
</v-flex>
The "v-for" is iterating through an array
of objects
, which have different properties: “v-for” 正在遍历一个objects
array
,这些对象具有不同的属性:
data () {
return {
months: [
{
month: 'Gen',
target: ''
},
{
month: 'Feb',
target: ''
},
and so on.
How can I conditionally apply the class underlined
in the first code block, so that can underline only some months and not all of them?如何有条件地应用在第一个代码块中underlined
的类,以便只能在几个月而不是全部下划线?
Thank you in advance!先感谢您!
Just use :class = '[{"className": X}]'
.只需使用:class = '[{"className": X}]'
。 Note the :
immediately before the class attribute.请注意:
紧接在 class 属性之前。
where, X is a computed / data property in the vue component that is Boolean.其中,X 是 vue 组件中的计算/数据属性,它是布尔值。 True will add the class and False won't. True 将添加类,而 False 不会。
className is your css classname. className 是你的 CSS 类名。
I believe you can do something like this:我相信你可以做这样的事情:
<router-link :class="{'underlined': month.shouldUnderline}" :to="month.target">{{(month.month)}}</router-link>
Let me know if that works!让我知道这是否有效!
Edit: more info here (also mentions multiple classes): https://v2.vuejs.org/v2/guide/class-and-style.html编辑:这里有更多信息(也提到了多个类): https ://v2.vuejs.org/v2/guide/class-and-style.html
While both the other answers are correct, if i understood correctly you want both classes - the fixed and a dynamic one.虽然其他两个答案都是正确的,但如果我理解正确,你想要两个类——固定的和动态的。
You can achieve this by doing:您可以通过执行以下操作来实现:
<router-link class="decoration" :class="{ underlined: CONDITION }" :to="month.target">{{(month.month)}}</router-link>
You can easily set this condition on a method:您可以轻松地在方法上设置此条件:
methods: {
isUnderlined() { return SOME IF STATMENT OR SOMETHING }
}
Do not wrap the :class with brackets [] like the other answer states, as it expects an object {}不要像其他答案状态那样用方括号 [] 包裹 :class,因为它需要一个对象 {}
你可以这样做:
<td :class="props.item[3] == 'pawned' ? 'text-red' : ''">{{ props.item[2] }}</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.