[英]value is undefined when passed from child emit in vue.js
我不確定為什么會發生這種情況,因為我的兩個方法(父和子都被調用)但是實際數據沒有被傳遞,即使我嘗試傳遞一個簡單的字符串,當從我的父母從發射接收數據:(
有人可以幫助我或者提供解釋嗎?
我的應用程序在 dom 中呈現為這樣(這都是 html 注入而不是 vue-cli 的一部分)
<div id="app">
<div>
<b-button v-b-modal.modal-1>upload files</b-button>
<b-modal id="modal-1" title="BootstrapVue" ok-title="upload">
<div>
<file-uploader v-on:child-to-parent="logChildData()"></file-uploader>
</div>
</b-modal>
</div>
</div>
這是我的子組件
//component constructor
var fileUploader = Vue.extend({
template: ' <div><div id="container" class="flexContainer"><div v-for="img in images" :key="img.name"><div :id=img.name><img height="80px" width="80px" :src=img.src alt="uploaded image"><button @click="removeItem(img.name)">X</button></div></div></div><input type="file" multiple @input="handleUpload($event)"></div>',
data() {
return {
images:[],
pdfs:[],
}
},
methods: {
//send files back up to parent component where they can be manipulated
sendDatatoParent(){
console.log('sending data to parent')
this.$emit('child-to-parent', 'a string to send')
},
async handleUpload(e) {
//check file type and sort into pdfs or images based on their file extension
var files = e.target.files
for(i = 0; i< files.length; i++){
if(files[i].type === 'application/pdf'){
this.pdfs.push(files[i])
}else{
if(files[i].type === 'image/jpeg' || files[i].type === 'image/png' || files[i].type === 'image/gif' || files[i].type === 'image/jpg'){
//create the thumbnail img and add it to the image object
//console.log(files[i].name)
this.buildUrl(files[i])
}
}
}
//console.log('pdfs',this.pdfs)
//console.log('images',this.images)
this.sendDatatoParent()
},
},
})
// register the file uploader component inside your app so your app has a name to reference the component by
Vue.component('file-uploader', fileUploader)
這是我的父母,我應該能夠從 $emit 記錄數據
// create the main vue app (this contains and renders all your components, it is the parent)
new Vue({
el: '#app',
methods:{
/*
listens for the custom $emit event (childToParent) which is passed to your child component (see above ^)
when fired it will grab the child's data and pass the value here (the parent component)
*/
logChildData(value){
console.log('logging the value from emit',value)
}
}
})
在父級的子組件標記中,您正在調用父級 function 沒有參數,因此該值將不存在。 通過$event
或完全刪除括號(推薦)。 這樣做:
v-on:child-to-parent="logChildData"
或者:
v-on:child-to-parent="logChildData($event)"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.