i have this input with onchange
event handler
JavaScript
x
2
1
<input accept="image/*" type='file' id="imgInp" @change="imageChange" />
2
my imageChange
method is like
JavaScript
1
11
11
1
methods : {
2
imageChange(e ){
3
const file = e.target.files[0];
4
console.log(file);
5
URL.createObjectURL(file);
6
this.model.image = URL.createObjectURL(file);
7
8
} ,
9
10
}
11
basically setting model.image
value on change now i want to have multiple images on my model … so i want to change the image property to array and have an input for each image
JavaScript
1
4
1
<div v-for="(image , index) in model.images" v-bind:key="index">
2
<input accept="image/*" type='file' id="imgInp" @change="imageChange" />
3
</div>
4
now i need to send the imaeg index in the array to my function as well so i can replace that index image like
and change my method to
JavaScript
1
9
1
imageChange(e , index ){
2
console.log(`event ->`);
3
console.log(e);
4
console.log(`index ->`);
5
console.log(index);
6
//URL...
7
this.model.images[index] = URL.createObjectURL(file);
8
}
9
but this will result in undefined event here is the output
JavaScript
1
5
1
event ->
2
undefined
3
index ->
4
0
5
how can i send extra argument to event handler without losing event argument ?
Advertisement
Answer
Just execute the function inside with @change
event, and pass the Vue provided $event
params first then index
param.
JavaScript
1
4
1
<div v-for="(image , index) in model.images" v-bind:key="index">
2
<input accept="image/*" type='file' id="imgInp" @change="imageChange($event, index)" />
3
</div>
4