Home  >  Q&A  >  body text

v-bind:style directive does not work with conditional comparisons

I can bind classes to elements in vue based on the following conditions:

:class="i18n.global.locale == 'ar' ? 'class1': 'class2'"

But when I try to use the same conditional binding style like this:

:style="[i18n.global.locale == 'ar' ? { 'background': 'blue;' }: { 'background': 'red' } ]"
None of

styles are applied to this element. Why doesn't the :style directive work?

Both directives apply to the container div that contains all other elements.

P粉321676640P粉321676640183 days ago323

reply all(1)I'll reply

  • P粉722409996

    P粉7224099962024-04-01 14:41:15

    I did some research on your problem, so to solve it you can do the following:

    1. Check the style of the element in the browser dev tools, it may happen that the bound style in the css file is overridden, for example, if you have a link to the css file
    2. Here { 'Background': 'Blue;' } Tried removing the ";", for me it started working when I removed it.

    reply
    0
  • Cancelreply