2017-03-28 24 views
7

मैं एक Vue घटक कॉल imageUpload बनाया और वी-मॉडल के रूप में संपत्ति पारित किया है नहीं है

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

और imgeUpload घटक भीतर मैं इस कोड है

<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]) 
    } 
}  

और मैं

प्राप्त

Uncaught TypeError: _this.$emit is not a function

धन्यवाद

+0

वसा तीर कार्यों के साथ 'विधियों' को परिभाषित न करें। http://stackoverflow.com/documentation/vue.js/9350/using-this-in-vue/28958/wrong-using-an-arrow-function-to-define-a-method-that-refers-to- यह # टी = 201703281535474331805 – Bert

+0

बहुत बढ़िया, धन्यवाद :) –

उत्तर

13

एक वसा तीर के साथ अपने विधि को परिभाषित करें। उपयोग:

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

    } 
} 

जब आप एक वसा तीर के साथ अपने विधि को परिभाषित, आप शाब्दिक गुंजाइश, thiswindow की ओर इशारा करते हो जाएगा, और नहीं Vue जिसका मतलब है पर कब्जा।

संबंधित मुद्दे