1

Just beginning with Vue soooo...be gentle with me! :) Situation: I have 2 different buttons which I would like to set/unset an active class on another element, 1 button sets the class and the other should remove the active class. I'm struggling with the method side (i think...). Code is from VueStoreFront.

<SfButton
        class="sf-button--text navbar__grid-view"
        @click="listView = false"
      >
        <SfIcon size="32px" color="#BEBFC4" icon="tiles" />
      </SfButton>
      <SfButton
        class="sf-button--text navbar__list-view"
        @click="listView = true"
      >
        <SfIcon size="32px" color="#BEBFC4" icon="list" />
      </SfButton>

The element where the class should be set and removed

<div :class="'products ' + (listView ? 'active' : '')" >Lorem Ipsum</div>

Then in methods i have

methods: {
listView(){
   this.active=false
},

Can anyone put me in the right direction? Thanks in advance!

1 Answer 1

1

you can read about vue class and style binding in the link below:

class and style binding in vue

but for short here is how you can do it:

<div :class="{'class-name': condition}"></div>

in the above example if the condition is true vue adds 'class-name' to the element and if it is false vue removes the class. so in your data object you can set the condition:

data() {
  condition: true,
}

and in your template you can have a button to control the condition value:

<button @click="condition = false"></button>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.