在Vue中,我们可能有多个选项来为元素设置条件类。

huangapple go评论61阅读模式
英文:

Conditional classes on elements in Vue where we may have multiple options

问题

<div :class="{
    'class-option1': record.styleOption === 'option1',
    'class-option2': record.styleOption === 'option2',
    'class-option3': record.styleOption === 'option3'
}">
英文:

I'm trying to add conditional classes to a div in Vue where I can have up to 3 class options based on the value of the bound data.

I think I need something like what I have below, but obviously the syntax is incorrect.
So in the example 'class-option1' is the class I'd like to apply where the bound data {{record.styleOption}} is equal to the value 'option1', and so on.

Can anyone suggest what needs changed?

&lt;div :class=&quot;{
    &#39;class-option1&#39;: {{record.styleOption}} === &#39;option1&#39;,
    &#39;class-option2&#39;: {{record.styleOption}}=== &#39;option2&#39;,
    &#39;class-option3&#39;: {{record.styleOption}}=== &#39;option3&#39;,
}&quot;&gt;

答案1

得分: 1

只需移除绑定数据周围的 {{ }}

&lt;div :class="{
    'class-option1': record.styleOption === 'option1',
    'class-option2': record.styleOption === 'option2',
    'class-option3': record.styleOption === 'option3',
}">
英文:

just remove {{ }} arround the bound data:

&lt;div :class=&quot;{
    &#39;class-option1&#39;: record.styleOption === &#39;option1&#39;,
    &#39;class-option2&#39;: record.styleOption === &#39;option2&#39;,
    &#39;class-option3&#39;: record.styleOption === &#39;option3&#39;,
}&quot;&gt;

huangapple
  • 本文由 发表于 2023年6月15日 17:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定