Vue Academy #2: V-model directive

Welcome to the second vue academy ! It will be a list of lot of article on vue! I have 2.5 years of experience in this and I can teach a few thing about this !

For this course we will focus on v-model directive !

Let’s start

First problema…


This content originally appeared on DEV Community and was authored by CodeOzz

Welcome to the second vue academy ! It will be a list of lot of article on vue! I have 2.5 years of experience in this and I can teach a few thing about this !

For this course we will focus on v-model directive !

Let's start

First problematic, how do we manage an input value in <input> ?

We could do the next :

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

<template>
<div>
    <input
        :value="message"
        @input="updateMessage"
        >
</div>
</template>

We need to bind value of input to our current data message and handle event from this input in order to update our current data message.

It's not really friendly and we have to do this for every component...

v-model

You can use the v-model directive to create two-way data bindings on form input, textarea, and select elements. It automatically picks the correct way to update the element based on the input type.

So we can replace the code above by

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

<template>
<div>
    <input v-model="message">
</div>
</template>

We can remove the method that update value ! Since v-model will directly update it.

It's very useful !


This content originally appeared on DEV Community and was authored by CodeOzz


Print Share Comment Cite Upload Translate Updates
APA

CodeOzz | Sciencx (2021-07-09T15:25:06+00:00) Vue Academy #2: V-model directive. Retrieved from https://www.scien.cx/2021/07/09/vue-academy-2-v-model-directive/

MLA
" » Vue Academy #2: V-model directive." CodeOzz | Sciencx - Friday July 9, 2021, https://www.scien.cx/2021/07/09/vue-academy-2-v-model-directive/
HARVARD
CodeOzz | Sciencx Friday July 9, 2021 » Vue Academy #2: V-model directive., viewed ,<https://www.scien.cx/2021/07/09/vue-academy-2-v-model-directive/>
VANCOUVER
CodeOzz | Sciencx - » Vue Academy #2: V-model directive. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/09/vue-academy-2-v-model-directive/
CHICAGO
" » Vue Academy #2: V-model directive." CodeOzz | Sciencx - Accessed . https://www.scien.cx/2021/07/09/vue-academy-2-v-model-directive/
IEEE
" » Vue Academy #2: V-model directive." CodeOzz | Sciencx [Online]. Available: https://www.scien.cx/2021/07/09/vue-academy-2-v-model-directive/. [Accessed: ]
rf:citation
» Vue Academy #2: V-model directive | CodeOzz | Sciencx | https://www.scien.cx/2021/07/09/vue-academy-2-v-model-directive/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.