Tuesday, January 2, 2018

8. Counter using buttons with v-on click

We can listen to an event using v-on:event, or its shorthand @event. The particular event we listen for is click.


This is App.vue file:


<template>
  <div>
    <button v-on:click="inc">Calling inc method</button>
    <button v-on:click="count++">inc expression</button>
    <button @click="dec">Calling dec method</button>
    <button @click="count--">dec expression</button>
    <h2>Counter: {{count}}</h2>
  </div>
</template>

<script>
export default {
  data() {
    return {
      count: 0
    }
  },
  methods: {
    inc() { this.count++},
    dec() { this.count--}
  }
}
</script>

<style>
h2 {
  color: blue;
  background-color: orange;
  width: 400px;
  margin: 50px auto;
  text-align: center;
}
button {
  color: white;
  background-color: green;
  border-radius: 10px;
  padding: 5px;
  display: block;
  margin: 0 auto;
}
</style>

This is the output:


No comments:

Post a Comment