search

Home  >  Q&A  >  body text

How to implement conditional class binding in VUE

Is it possible to create a condition using Vue to add a class based on the text loaded on the doom changes?

I have a list with color names

<span>yellow</span>
<span>red</span>
<span>grey</span>

I want to set a condition, for example if the name text is yellow I want to add the class "color_yellow" etc.

P粉099985373P粉099985373375 days ago420

reply all(1)I'll reply

  • P粉627136450

    P粉6271364502024-03-30 09:51:33

    Try a code snippet like the following:

    new Vue({
      el: '#demo',
      data() {
        return {
          colors: ['yellow', 'red', 'grey']
        }
      }
    })
    
    Vue.config.productionTip = false
    Vue.config.devtools = false
    .color_yellow {
      color: yellow;
    }
    .color_red {
      color: red;
    }
    .color_grey {
      color: grey;
    }
    sssccc
    
    • {{ color }}

    reply
    0
  • Cancelreply