0

i'm getting an error in the below code saying this is undefined.

<div class="location-list__item" v-for="(value, key) in locations.data">
    <div class="location-list__item--text"
         :class="{ selected: selected === key }"
         @click="() => { this.selected = key; this.manageSurrounding = false }">
        <i class="fas fa-compass"></i> {{ value.name }}
        <span v-if="value.changed" class="has-text-danger"> Changed</span>
    </div>
</div>

However if I change this line:

@click="() => { this.selected = key; this.manageSurrounding = false }"

to this

@click="selected = key"

It works fine, however I need to change manageSurrounding at the same time and I don't want to create a method for such a simple thing.

3
  • this is not in template scope, just use: @click="() => { selected = key; manageSurrounding = false }" Commented Jun 12, 2020 at 10:27
  • @ZedHome This doesn't work at all, already tried Commented Jun 12, 2020 at 10:42
  • What is the error? Commented Jun 12, 2020 at 11:18

3 Answers 3

5

You can do multiple assignments by using semicolon like the above statement which you have written.

<div class="location-list__item" v-for="(value, key) in locations.data">
    <div class="location-list__item--text"
         :class="{ selected: selected === key }"
         @click="selected = key;manageSurrounding = false">        # Like this
        <i class="fas fa-compass"></i> {{ value.name }}
        <span v-if="value.changed" class="has-text-danger"> Changed</span>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

> Statements are not allowed in Vue expressions
@MartynBall That's not true. Why do you think that? See demo of the answer above.
@tony19 that's the error I get when I attempt to do what you say
@MartynBall Where are you seeing the error? If it's Webstorm, that's not actually a real error.
1

You can use a anonymous function like,

<div onclick="return function()

 { selected = key; manageSurrounding = false }'

</div>

Comments

0

Just create a method and put in the update lines, you are better off on the long run, if your list is changing/reordering/re-rendering often.

It’s an optimization opportunity, so don’t try to force it in just because it seems small. Have a look at this answer: anonymus function in template

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.