v-model Vs :value in Vue.js

Know how to use v-model and :value @input in Vue with exampels

June 29, 2024

Why Use v-model?

v-model simplifies the binding between the input elements and the Vue instance's data. It automatically updates the data property when the input value changes and updates the input value when the data property changes. This two-way data binding is particularly useful for form inputs, as it reduces boilerplate code and keeps the data and UI in sync.

Difference Between v-model and :value

  • :value: This binds the value of an input element to a data property in a one-way fashion. If you change the data property, the input element's value will update, but changing the input element's value will not update the data property.
  • v-model: This creates a two-way binding. It binds the value of the input element to a data property and ensures that changes to either the input element's value or the data property are reflected in both places.

Examples

Using v-model

<template>
  <div>
    <input v-model="message" placeholder="Type something" />
    <p>{{ message }}</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        message: "",
      };
    },
  };
</script>

Example 2: Using :value and @input

<template>
  <div>
    <input
      :value="message"
      @input="updateMessage"
      placeholder="Type something"
    />
    <p>{{ message }}</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        message: "",
      };
    },
    methods: {
      updateMessage(event) {
        this.message = event.target.value;
      },
    },
  };
</script>

Updating Another State Based on User Input

If you have an input field or a select box and need to update another state based on the user's input, you can achieve this by watching the data property or by using methods to handle the input events.

Example with Input

<template>
  <div>
    <input v-model="message" placeholder="Type something" />
    <p>{{ message }}</p>
    <p>Updated State: {{ updatedState }}</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        message: "",
        updatedState: "",
      };
    },
    watch: {
      message(newVal) {
        this.updatedState = newVal.toUpperCase(); // Example transformation
      },
    },
  };
</script>

Example with Select

<template>
  <div>
    <select v-model="selectedOption">
      <option value="option1">Option 1</option>
      <option value="option2">Option 2</option>
    </select>
    <p>Selected Option: {{ selectedOption }}</p>
    <p>Updated State: {{ updatedState }}</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        selectedOption: "",
        updatedState: "",
      };
    },
    watch: {
      selectedOption(newVal) {
        this.updatedState = `You selected: ${newVal}`;
      },
    },
  };
</script>

Update another state based on user input using methods

<template>
  <div>
    <label for="inputField">Input Field:</label>
    <input
      type="text"
      id="inputField"
      v-model="userInput"
      @input="updateAnotherState"
    />

    <p>Another State: {{ anotherState }}</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        userInput: "", // State to store user input
        anotherState: "", // Another state to be updated based on user input
      };
    },
    methods: {
      updateAnotherState() {
        // Logic to update anotherState based on userInput
        this.anotherState = `Updated with: ${this.userInput}`;
      },
    },
  };
</script>

In this example:

We have an <input> field bound to userInput using v-model. The updateAnotherState method is called whenever the input event is triggered on the input field (@input="updateAnotherState"). Inside updateAnotherState, you can implement any logic to update anotherState based on userInput. Here, it simply concatenates a string with userInput. This pattern allows you to reactively update anotherState whenever userInput changes, using Vue.js's reactivity system. Adjust the logic inside updateAnotherState as per your specific requirements.