0

I have created a method that is triggered with a button on:click event, but it doesn't seem to work.

There is the code:

<template>
  <div class=button>
        <button v-on:click="refreshTemperature"  type="button"  > Search </button>
        <p> Temperature : {{ temp }} </p>
  </div>
</template>

export default {
  name: 'Meteo',
  data() {
    return {
      temp: Math.floor(Math.random() * (30 - 10) ) + 10,
  },
  methods: {
      refreshTemperature : () => {
           this.temp ++;

        }
}

When I click the button nothing happens. What am I missing?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kehalS
  • 15
  • 5
  • You're missing a }. There should be one to end the `methods` object – AlexH Oct 21 '20 at 18:44
  • forgot it when i copyed the code but on my source there is the brackets ;) that's not the problem unfortunately @AlexH – kehalS Oct 21 '20 at 18:46

1 Answers1

2

Don't use the arrow function, but the regular function. You can refer to this link.

<template>
  <div class=button>
        <button @click="refreshTemperature"  type="button"  > Search </button>
        <p> Temperature : {{ temp }} </p>
  </div>
</template>

<script>
export default {
  name: 'Meteo',
  data() { 
    return {
      temp: Math.floor(Math.random() * (30 - 10) ) + 10,
    }
  },
  methods: {
      refreshTemperature() {
           this.temp ++;
          
        }
  }
}
</script>
tony19
  • 125,647
  • 18
  • 229
  • 307
wangdev87
  • 8,611
  • 3
  • 8
  • 31