Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a correct usage of v-on:change?

I have a text input that has a v-model and a v-on:change attached to it. As the user types I update an array in data and the UI is bound to that array. I also want to call a method to send the user input via AJAX. The data sent to the server is the result of a computed property.

<div id="app">
  <input
      id="user-input"
      type="text"
      v-model="userInput"
      v-on:change="process()">

   <ul id="parsed-list">
      <li v-for="item in parsedInput">
          {{ item }}
      </li>
    </ul>
</div>

let parse = input => {
    return input.split(',')
}

let serverProcess = values => {
    // Send array to server
}

new Vue({
  el: '#app',
  data: {
    userInput: ''
  },
  computed: {
    parsedInput () {
        return parse(this.userInput)
    }
  },
  methods: {
    process () {
        serverProcess(this.parsedInput);
    }
  }
});

Is this usage of both a v-model and v-on:change together best practice Vue?

like image 461
user9993 Avatar asked Dec 18 '17 14:12

user9993


People also ask

What is the use of V-on?

The v-on:click directive is a Vue. js directive used to add a click event listener to an element. First, we will create a div element with id as app and let's apply the v-on:click directive to a element. Further, we can execute a function when click even occurs.

Where can I use v-model?

A common use case for using v-model is when designing forms and inputs. We can use it to have our DOM input elements be able to modify the data in our Vue instance. Let's look at a simple example that uses a v-model on a text input. When we type in our text input, we'll see that our data property is changing.

Can I use V-for with V-if?

It's not recommended to use v-if and v-for on the same element due to implicit precedence. Refer to style guide for details.

What is v-model short for?

The V-model is an SDLC model where execution of processes happens in a sequential manner in a V-shape. It is also known as Verification and Validation model. The V-Model is an extension of the waterfall model and is based on the association of a testing phase for each corresponding development stage.


1 Answers

thumbs up @kmc0590000. In addition, with watch you can also see the previous state and current. They are passed as parameters.

v-model is just syntactic sugar and does the following:

<input :value="userInput" @input="change">

You can also write @change instead of v-on: and :value instead of v-bind:value.

In Line 6 (v-on:change="process()") you can remove the parentheses. The input event is given to your method as parameter and you can access the attributes directly. (https://vuejs.org/v2/guide/events.html#Method-Event-Handlers)

like image 112
Phil Avatar answered Nov 14 '22 06:11

Phil