How do you implement two-way data binding in a Vue.js component?

devquora
devquora

Posted On: Jun 20, 2024

 

To implement two-way data binding in a Vue.js component, you can use the "v-model" directive. This directive binds a value to a form input element, and updates the value whenever the input element's value changes.

Here is an example of how you might use the "v-model" directive in a Vue.js component:

<template>
  <div>
   <input v-model="message" type="text">
   <p>{{ message }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello world!'
    }
  }
}
</script>

In this example, the value of the "message" data property is bound to the value of the input element using the "v-model" directive. When the user types in the input element, the value of the "message" data property is updated, and this change is reflected in the text displayed by the p element.

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Vue.js Interview Questions

    What is Vue.js?

    Vue.js is a progressive JavaScript framework that simplifies UI development. It offers reactive data binding, component-based architecture, and a flexible ecosystem. Vue's simplicity and versatility m..

    Vue.js Interview Questions

    List some features of Vue.js.

    Vue.js offers templates for easy UI creation, reactive data binding, component-based architecture, built-in transitions for animations, and dynamic routing for single-page applications. These features..

    Vue.js Interview Questions

    Explain Life cycle of Vue Instance.

    The Vue.js lifecycle includes stages like creation for initialization, mounting for DOM insertion, updating for reactivity, and destroying for cleanup. Each stage provides hooks to manage component be..