Tuesday, January 2, 2018

9. Chaging Counter background color using v-model and binding (v-bind) to style

An input box controls style of the Counter display background color. The input uses v-model for 2-way data binding, and the style attribute is bound using v-bind to that background color.


This is App.vue file.


<template>
  <div>
    <button @click="inc">Calling inc method</button>
    <button @click="dec">Calling dec method</button>
    <h2 v-bind:style="{backgroundColor:col}">Counter: {{count}}</h2>
    <input type="text" v-model="col">background counter color</input>
  </div>
</template>

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

<style>
h2 {
  color: blue;
  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