Vue js: _this. $ Emit is not a function

I created a Vue component for the imageUpload component and passed the property as v-model

<image-upload v-model="form.image"></image-upload>

and inside the imgeUpload component I have this code

<input type="file" accept="images/*" class="file-input" @change="upload">

 upload:(e)=>{ const files = e.target.files; if(files && files.length > 0){ console.log(files[0]) this.$emit('input',files[0]) } } 

and i got

Uncaught TypeError: _this. $ emit is not a function

thanks

+7
vuejs2 vue-component
source share
1 answer

Do not define your method with a thick arrow. Using:

 upload: function(e){ const files = e.target.files; if(files && files.length > 0){ console.log(files[0]) this.$emit('input',files[0]) } } 

When you define your method with a thick arrow, you are fixing the lexical region, which means that this will point to window , not Vue.

+13
source share

All Articles