[Vuejs]-When should I use key vs. v-model for an input?

0👍

Well, usually you need a key in lists and the keys need to come from a source so that a key of a given entry does not change over the time.
If you use a UUID generator, the keys will change at every re-render and you don’t want this. Therefore the best solution is to use something like json-server for mocking things up or using a real backend api.

There are situations like with OAuth2 where by default you cannot send a POST object with Content-type application/json and the data in the body object since the protocol expects something www-form-urlencoded.

In this case you need something like

<form ref="someForm">
  <input name="nameofinput" ref="myFancyInput" />
</form>

this way by accessing this.$refs you can do something like:

this.$refs["myFancyInput"]="somevalue"
this.$refs["someForm"].submit();

The contents of the input’s value can be also a JSON, since JSON is just a string formatted in a given way.

Then in the backend you can process that as you want.

http://www.davidepugliese.com/csv-download-ajax-and-spring/

As for the v-model annotation, that is just shortcut for something like:

<input :value="someVar" @input="functionName($event)" />


functionName(event) {
   console.log(event)

}

Leave a comment