Javascript Vue: Where does the variable e
in onFileChange(e)
originate?
In the following code, there is a variable e
in onFileChange(e)
, where does it originate? It is never declared or imported in the code, so how can it be valid?
Any help would be greatly appreciated.
JavaScript
x
83
83
1
<template>
2
<div class="container" style="margin-top: 50px;">
3
<div class="text-center">
4
<h4>File Upload with VueJS and Laravel</h4>
5
<br />
6
<div style="max-width: 500px; margin: 0 auto;">
7
<div v-if="success !== ''" class="alert alert-success" role="alert">
8
{{success}}
9
</div>
10
<form @submit="submitForm" enctype="multipart/form-data">
11
<div class="input-group">
12
<div class="custom-file">
13
<input
14
type="file"
15
name="filename"
16
class="custom-file-input"
17
id="inputFileUpload"
18
v-on:change="onFileChange"
19
/>
20
<label class="custom-file-label" for="inputFileUpload"
21
>Choose file</label
22
>
23
</div>
24
<div class="input-group-append">
25
<input type="submit" class="btn btn-primary" value="Upload" />
26
</div>
27
</div>
28
<br />
29
<p class="text-danger font-weight-bold">{{filename}}</p>
30
</form>
31
</div>
32
</div>
33
</div>
34
</template>
35
36
<script>
37
export default {
38
mounted() {
39
console.log("Component successfully mounted.");
40
},
41
data() {
42
return {
43
filename: "",
44
file: "",
45
success: ""
46
};
47
},
48
methods: {
49
onFileChange(e) {
50
//console.log(e.target.files[0]);
51
this.filename = "Selected File: " + e.target.files[0].name;
52
this.file = e.target.files[0];
53
},
54
submitForm(e) {
55
e.preventDefault();
56
let currentObj = this;
57
const config = {
58
headers: {
59
"content-type": "multipart/form-data",
60
"X-CSRF-TOKEN": document.querySelector('meta[name="csrf-token"]')
61
.content
62
}
63
};
64
65
// form data
66
let formData = new FormData();
67
formData.append("file", this.file);
68
69
// send upload request
70
axios
71
.post("/store_file", formData, config)
72
.then(function(response) {
73
currentObj.success = response.data.success;
74
currentObj.filename = "";
75
})
76
.catch(function(error) {
77
currentObj.output = error;
78
});
79
}
80
}
81
};
82
</script>
83
Advertisement
Answer
That declaration is triggered by your template, where you are binding change
event to the method. The whole event as parameter gets passed to the method, Refer this section of Vue docs
for better information https://v2.vuejs.org/v2/guide/events.html#Method-Event-Handlers