0

I have a v-for loop like this

<div v-for="deal in deals">
  <div class="title">{{deal.title}}</div>
</div>

I'm trying to set a variable of a hover state, this is what I tried:

<div v-for="deal in deals" @mouseover="deal.hover = true" @mouseout="deal.hover = false">
  <div class="title">{{deal.title}}</div>
  <div class="description" v-if="deal.hover">{{deal.description}}</div>
</div>

Note that deal.hover is undefined by default (and it can't be pre-defined as false).

Is something like this possible in VueJS?

1
  • Why it can't be pre-defined as false ? Commented May 5, 2020 at 18:09

1 Answer 1

1

I'm not sure what you mean by "temporary" variable. Your code is attempting to add a hover property to each deal. You can certainly do that.

<div v-for="deal in deals"
     @mouseover="$set(deal, 'hover', true)"
     @mouseout="$set(deal, 'hover', false)"
  <div class="title">{{deal.title}}</div>
  <div class="description" v-if="deal.hover">{{deal.description}}</div>
</div>

Note the use of $set to add a reactive property to an object (see https://v2.vuejs.org/v2/guide/reactivity.html#For-Objects).

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.