Alpine.js类绑定与多个条件

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

alpine js class binding with multiple condition

问题

我需要根据多个条件绑定类。

:class="showMenu === true ? 'bg-chevron' : 'bg-chevron-blue bg-contain', showDetail === true ? 'rotate-180' : ''"

我根据showMenu使用不同颜色的图标,并根据showDetail旋转它们。我使用逗号分隔它们,但只有第一个条件起作用。

英文:

I need to bind classes with multiple condition.

:class="showMenu === true ? 'bg-chevron' : 'bg-chevron-blue bg-contain', showDetail === true ? 'rotate-180' : ''"

I use different colored icons according to showMenu and they should rotate according to showDetail. I seperate them with comma but only first condition works

答案1

得分: 2

在多类绑定中,您需要在[]中使用三元条件。

:class="[
  showMenu === true ? 'bg-chevron' : 'bg-chevron-blue bg-contain',
  showDetail === true ? 'rotate-180' : ''
]"

请注意,这是HTML代码的一部分,所以不需要额外的翻译。

英文:

For multiple class bindings you need to use ternary conditions with in []

:class="[showMenu === true ? 'bg-chevron' : 'bg-chevron-blue bg-contain', showDetail === true ? 'rotate-180' : '']"

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.js&quot;&gt;&lt;/script&gt;
&lt;div x-data=&quot;{showMenu:true, showDetail: true}&quot;&gt;
  &lt;label for=&quot;name&quot; :class=&quot;[(showMenu === true ? &#39;bg-chevron&#39; : &#39;bg-chevron-blue bg-contain&#39;),(showDetail === true ? &#39;rotate-180&#39; : &#39;&#39;)]&quot;  &gt;Name:&lt;/label&gt;
  &lt;input id=&quot;name&quot; type=&quot;text&quot; x-model=&quot;name&quot; /&gt;
  &lt;p x-text=&quot;name&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月8日 20:53:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386092.html
匿名

发表评论

匿名网友

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

确定