Condition in v-bind:Style - VueJS

后端 未结 8 2202
遥遥无期
遥遥无期 2020-12-23 19:28

I have an easy question for which I hope you could help:

相关标签:
8条回答
  • 2020-12-23 20:14

    Use condition in V-bind:style VueJS:

    v-bind:style= "[condition ? {styleA} : {styleB}]"
    

    Here is the minimal example,

    <figure :style="[item.main_featured ? {'background': 'url(' + item.main_featured + ') center no-repeat'} : {'background': '#FFF'}]">
    

    If you need Parent-Child-Conditions, then this is the magic:

    v-bind:style= "[condition_1 ? condition_2 ? {styleA} : {styleB} : {styleC}]"
    

    In short of:

    if (condition_1) {
       if (condition_2) {
          return styleA
       } else {
          return styleB
       }
    } else {
      return styleC
    }
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-23 20:16

    this worked for me

    <div :style="[ myboolean ? { backgroundImage: `url(${group.backgroundImage.sourceUrl})` } : null ]">
    
    0 讨论(0)
提交回复
热议问题