gpt4 book ai didi

vue.js @emit is not picked up by parent(Vue.js@emit未被家长接收)

转载 作者:bug小助手 更新时间:2023-10-25 13:12:03 26 4
gpt4 key购买 nike



In my app I have a form, in the ManageCards component, that conatins a child component ImageUpload, that emits and imagefile and its local URL to the parent:

在我的应用程序中,在ManageCards组件中有一个表单,它包含子组件ImageUpload,该组件将发出和图像文件及其指向父组件的本地URL:


<form  class="mb-3" > 
<div class="form-group">
<h4>{{card.description}}</h4>
<textarea class="form-control" :placeholder="card.description" v-model="card.description" />
</div>
<div>
<img v-if="card.picture" class="img-circle" style="width:150px" v-bind:src="card.picture" />
</div>
<div id="preview">
<h4>Preview</h4>
<ImageUpload :imgUrl="url" @newPicture="updatePicture"></ImageUpload>
<button @click="addCard" type="button" class="btn btn-primary btn-block" style="color:white">Save</button>
</div>
</form>

De addCard method calls a POST command to send information to the API:

De addCard方法调用POST命令向接口发送信息:


addCard() {
const formData = new FormData();
formData.append('title',this.card.title);
formData.append('description',this.card.description);
formData.append('imagefile',this.selectedFile);
formData.append('game_id',this.currentGame.id);
// ...
}

SelectedFile and card.picture should be populated by the emitted method updatePicture:

SelectedFile和card.Picture应该由发出的方法updatePicture填充:


updatePicture(imageData) {
console.log("emit ontvangen");
this.selectedFile = imageData.file;
this.card.picture = imageData.picture;
// ...
}

The ImageUpload Component itself looks like:

ImageUpload组件本身如下所示:


<template>
<div class="Image-Upload-wrapper Image-upload">
<div>
<input type="file" v-on:change="onFileChange" ref="fileUpload" id="file_picture_input" />
<label for="file_picture_input" class="upload_picture_button">Kies afbeelding</label>
</div>
<div id="croppie"></div>
<div class="upload-wrapper">
<button type="button" class="btn btn-primary btn-sm" v-on:click="setImage">
<!-- type="button"" anders vervest de knop de hele pagina " -->
Set image
</button>
</div>
</div>
</template>

<script>
// uitleg over Croppie::https://www.youtube.com/watch?v=kvNozA8M1HM
import Croppie from 'croppie';

export default {
props: [
'imgUrl'
],
mounted() {
this.setUpCroppie()
},
data() {
return {
croppie: null,
croppieImage: '',
selectedFile: '',
picture: '',
url: '',
}
},
methods: {
setUpCroppie() {
let el = document.getElementById('croppie');
this.croppie = new Croppie(el, {
viewport: { width: 200, height:200, type:'circle' },
boundary: { width: 220, height:220 },
showZoomer: true,
enableOrientation: true
});
this.croppie.bind({
url: this.url
});
},
setImage() {
this.croppie.result({
type: 'canvas',
size: 'viewport'
})
.then(blob =>fetch(blob))
.then(res => res.arrayBuffer)
.then(buf => new File([buf], this.croppieImage.name, { type:'image/png' }))
.then(file => {
this.croppieImage = file;
this.picture = URL.createObjectURL(this.croppieImage);
const imageData = {
picture: this.picture,
file: this.croppieImage
};
console.log("klaar voor emit...?");
this.$emit('newPicture', imageData);
this.url = '';
})
},
onFileChange(e) {
if (e.target.files && e.target.files.length > 0) {
this.selectedFile = e.target.files[0];
console.log("gekozen file is: " + this.selectedFile.name);
this.url = URL.createObjectURL(this.selectedFile);
this.croppie.bind({
url: this.url
});
} else {
// Geen afbeelding geselecteerd, instellen op een standaardwaarde (bijv. een lege string)
this.selectedFile = null;
this.url = '';
}
}
},
}
</script>

In the Console I do see: klaar voor emit...?, from the setImage method in the chold component.
I do not see the emit ontvangen from the emit method in the parent.
Consequently the required fields for the POST command are also not populated.

在控制台里我确实看到了:klaar voor emit.?,从chold组件中的setImage方法。我在父类中没有看到emit方法的emit属性。因此,POST命令所需的字段也未填充。


Can anyone see what (probably silly) mistake I am making?

有人能看到我在犯什么(可能很愚蠢的)错误吗?


更多回答

Your code looks fine at first glance, its functionality cannot be tested without reproduction. If I had to guess, you might not be observing the emit in the right place.

乍一看,您的代码看起来很好,其功能不能在没有复制的情况下进行测试。如果要我猜的话,你可能没有在正确的位置观察到排放。

From the Croppie NPM description, it is also apparent that there is an optimized version for Vue.js called vue-croppie. I assume they might have addressed inter-component communication here, or Vue's native solutions might already provide the necessary information. It's not inconceivable that the error you encountered with the original Croppie package could have been caused by DOM updates.

从Croppie NPM的描述来看,也很明显有一个针对Vue.js的优化版本,称为VUE-Croppie。我认为他们可能已经在这里解决了组件间通信,或者Vue的本机解决方案可能已经提供了必要的信息。您在原始Croppie包中遇到的错误可能是由DOM更新引起的,这并不是不可想象的。

优秀答案推荐

Child Element


A child element needs to declare the emits.

子元素需要声明emits。


Options API

export default {
emits: ['first-emit-name', 'second-emit-name'],
}

Composition API

const emit = defineEmits(['first-emit-name', 'second-emit-name'])

After this, it will forward the declared emits to the parent element in Vue.

在此之后,它会将声明的emits转发给Vue中的父元素。


Options API

this.$emit('first-emit-name')
this.$emit('second-emit-name')

Composition API

// After const emit declaration
emit('first-emit-name')
emit('second-emit-name')

Parent Element


<ChildElement @first-emit-name="(e) => ..." @second-emit-name="(e) => ...">

更多回答

this did not solve the problem.

这并没有解决问题。

The emit declaration is not mandatory I see in the documentation. Furthermore, the component works in another parent where I use it..

我在文档中看到的emit声明并不是强制性的。此外,该组件在我使用它的另一个父组件中工作。

I don't know what could be going wrong; I followed the documentation for what I wrote, and I've never had any issues or problems with it. Is it possible that the function responsible for triggering the emit isn't running?

我不知道可能出了什么问题;我遵循了我所编写的文档,并且我从来没有遇到过任何问题或问题。有没有可能是负责触发发射的功能没有运行?

Vue Playground - I've simplified your emit operation to the functions you believed were faulty. It's clear from the playground that there's nothing wrong with the emit. My response in this regard is correct. Your issue must be somewhere else in the code, for which you haven't provided reproduction, so I can't test it.

我已经将你的发射操作简化为你认为有故障的功能。从操场上可以清楚地看到,尾气没有任何问题。我在这方面的回答是正确的。您的问题一定在代码中的其他地方,您没有提供复制,所以我不能测试它。

Thanks for the response. Staring from a simplified version again seems to be a good idea. I will look into it after next week. I cannot share the whole code I think, that would become to elaborate for this platform I guess.

感谢您的回复。再次从简化版本入手似乎是个好主意。我会在下周之后调查这件事。我不能分享我认为的全部代码,我想这将成为这个平台的详细说明。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com