Vue 3 // v-model on custom input type=”file”

Hi everyone. Let’s get to the point. A few days ago I needed to use v-model in a component of my own that I needed to capture a file in an input of type file. After trying a bit, the solution was the following:

<template>

<input type=”fi…


This content originally appeared on DEV Community and was authored by Salvador García

Hi everyone. Let's get to the point. A few days ago I needed to use v-model in a component of my own that I needed to capture a file in an input of type file. After trying a bit, the solution was the following:

<template>

  <input type="file" @change="onChangeFile" name="file" />

</template>

<script>
  methods: {
    onChangeFile(event) {
      this.$emit('update:modelValue', event.target.files[0])
    }
</script>

//Your component made in Vue 3
<MyInputFileComponent v-model="file" />

This is the easiest way to use it. If you need to capture many files you just have to go through the list of files found in the e.target.files

I share this information in case there is anyone out there looking for a solution for this scenario. Happy dev.


This content originally appeared on DEV Community and was authored by Salvador García


Print Share Comment Cite Upload Translate Updates
APA

Salvador García | Sciencx (2021-09-01T23:53:14+00:00) Vue 3 // v-model on custom input type=”file”. Retrieved from https://www.scien.cx/2021/09/01/vue-3-v-model-on-custom-input-typefile/

MLA
" » Vue 3 // v-model on custom input type=”file”." Salvador García | Sciencx - Wednesday September 1, 2021, https://www.scien.cx/2021/09/01/vue-3-v-model-on-custom-input-typefile/
HARVARD
Salvador García | Sciencx Wednesday September 1, 2021 » Vue 3 // v-model on custom input type=”file”., viewed ,<https://www.scien.cx/2021/09/01/vue-3-v-model-on-custom-input-typefile/>
VANCOUVER
Salvador García | Sciencx - » Vue 3 // v-model on custom input type=”file”. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/01/vue-3-v-model-on-custom-input-typefile/
CHICAGO
" » Vue 3 // v-model on custom input type=”file”." Salvador García | Sciencx - Accessed . https://www.scien.cx/2021/09/01/vue-3-v-model-on-custom-input-typefile/
IEEE
" » Vue 3 // v-model on custom input type=”file”." Salvador García | Sciencx [Online]. Available: https://www.scien.cx/2021/09/01/vue-3-v-model-on-custom-input-typefile/. [Accessed: ]
rf:citation
» Vue 3 // v-model on custom input type=”file” | Salvador García | Sciencx | https://www.scien.cx/2021/09/01/vue-3-v-model-on-custom-input-typefile/ |

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.