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, do you have any idea what I'm missing? I'm really new to Vue.js.

NKSM
  • 4,623
  • 4
  • 20
  • 35
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>
wangdev87
  • 8,285
  • 3
  • 6
  • 30