Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why v-model is not working?

This is my very first code in Vue.js. I am following a simple online tutorial. After installing the application using the vue-cli, I created a simple component Test.vue which contains a simple input control bound to the message property of my model:

Test.vue

<template>
  <div 
    <input 
        type="text" 
        v-model="message"/>
    <br/>
    <p>The value of the input is: {{ message }}</p>
  </div>
</template>

<script>
    export default {  
      data:{
        message: 'My name'
      }
    };
</script>

<style>
</style>

Then I load this component inside the <App />. But when I write a text inside the input box, the <p> element is not updated...

enter image description here

What am I doing wrong? This looks pretty straightforward. Thanks you for your suggestions and pointing me to the right direction.

like image 623
TheSoul Avatar asked Dec 14 '17 23:12

TheSoul


1 Answers

In a component, data must be a function.

export default {  
  data(){
    return {
      message: 'My name'
    }
  }
};

Also, your template is missing a > in the first div, but I'm guessing that happened writing the question.

<template>
  <div>
    <input 
        type="text" 
        v-model="message"/>
    <br/>
    <p>The value of the input is: {{ message }}</p>
  </div>
</template>
like image 155
Bert Avatar answered Oct 21 '22 19:10

Bert