Vue

VueJS2: Section 2

by Aubrey Taylor

HTML

<div id="app">
  <input type="text" v-model="name" />
  <p>{{ name }}</p>
  
  
  <button v-on:click="counter++">Increase</button>
  <button v-on:click="counter--">Decrease</button>
  <p>Counter: {{ counter }}</p>
  <p>Result: {{ result() }} | {{ output }}</p>
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}

Vue

new Vue({
  el: "#app",
  data: {
    name: "Aubz",
    counter: 0,
    lastTimeoutId: false,
  },
  computed: {
  	output: function() {
    	return this.counter > 5 ? "5+" : "5-";
    }
  },
  watch: {
  	counter: function(value) {
    	const id = setTimeout(() => {
      	this.counter = 0;
      }, 2000);
			if(this.lastTimeoutId) clearTimeout(this.lastTimeoutId);
      this.lastTimeoutId = id;
    }
  },
  methods: {
  	result: function() {
    	return this.counter > 5 ? "5+" : "5-";
    }
  }
})