在VueJS组件中处理来自输入文件的事件

问题描述

我想更改图像的子组件:

<template>
    <div>
        <div>
            <form enctype="multipart/form-data">
                <input type="file" name="file" id="file" @change="change_file" class="py-6 px-8 hidden">
                <label for="file" class="flex flex-col items-center text-main-color text-sm py-2 px-4">
                    <span class="text-sm mt-2" style="color: #7eaeb7;">Click to change image</span>
                    <img :src="'/img/'+data.img" alt="" class="w-48 h-48 mx-auto p-4" >
                </label>
            </form>
        </div>

        <div class="flex flex-col items-center">
            <span class="">{{data.deFinition}}</span>
            <span>{{data.description}}</span>

            <div @click="edit_statment">
                <span>Edit Statment</span>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        props: ['data'],methods: {
            change_image(event) {
                let formData = new FormData();
                formData.append('file',event.target.files[0]);

                axios.post('/change_image',formData,{
                    headers: {
                      'Content-Type': 'multipart/form-data'
                    }
                })
                    .then(response => {
                        this.$emit('changeStatmentImg',response.data.new_image);
                    })
            },edit_statment() {
                this.$emit('editStatment');
            }
        }
    }
</script>

我在侦听父组件上的事件:

<template>
<div class="">
    <statment v-for="item in statments" :key="item.id" :data="item" @editStatment="initialize(item,'statment')" @changeStatmentImg="changeImage($event,item,'statment')"></pr_course_statment>
</div>
</template>
<script>
    export default {
        data() {
            return {
                statments: [
                    {
                        img: 'module1.svg',deFinition: 'First statment',description: 'First statment description',},{
                        img: 'module2.svg',deFinition: 'Second statment',description: 'Second statment description',{
                        img: 'module3.svg',deFinition: 'Third statment',description: 'Third statment description',],}
        },methods: {
            initialize(item,model_name) {
                console.log('item.id: ',item.id);  
            },changeImage(file_name,model_name) {
                console.log(file_name);
                console.log(item.id); 
                console.log(model_name);
            },}
    }
</script>

当我从组件发出事件时,如果对文本字段的(初始化方法)进行通用编辑,那么一切将顺利进行

但是,当子组件在“输入文件@change”上暴露事件时,它每次仅使用“语句”列表中的第一项

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">

<div id="app" class="flex justify-between">
      <div v-for="item in statments">
                <div class="flex flex-col items-center">
                    <div class="flex flex-col items-center border border-indigo-200 rounded-md">
                        <form class="flex justify-center my-4" enctype="multipart/form-data">

                            <input type="file" name="file" id="file" @change="changeImage(item)" class="py-6 px-8 hidden">
                            <label for="file" class="flex flex-col items-center text-main-color text-sm py-2 px-4">
                                <span class="text-sm mt-2" style="color: #7eaeb7;">Click to change image</span>
                                <img :src="item.img" alt="" class="w-48 h-48 mx-auto p-4" >

                            </label>
                        </form>

                    </div>

                    <div class="mt-4 mb-6" @click="changeStatment(item)" class="flex flex-col items-center">
                        <span>{{item.deFinition}}</span>
                        <span class="text-indigo-500 inline-flex items-center md:mb-2 lg:mb-0 main-color">Edit Statment
                            <svg class="w-4 h-4 ml-2" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                <path d="M5 12h14"></path>
                                <path d="M12 5l7 7-7 7"></path>
                            </svg>
                        </span>
                    </div>
                </div>  
            </div>
</div>

<script>
var app = new Vue({
  el: '#app',data() {
            return {
                statments: [
                    {
                        id: 1,img: 'module1.svg',{
                        id: 2,img: 'module2.svg',{
                        id: 3,img: 'module3.svg',methods: {
            changeStatment(item) {
                console.log('item.id: ',changeImage(item) {
                console.log('item.id from form input: ',item.id);
            },}
})
</script>

解决方法

我很确定,这是因为您的父组件:key="item.id"中有undefined

:key属性对于vue的内部呈现和缓存以及HAS在全球范围内是唯一的(不仅仅是THIS组件是唯一的)很重要。

我总是使用这样的东西:

<div v-for="(item,idx) in items" :key="`Items_${_uid}_${idx}`" />

_uid对于要渲染的每个vue组件都会增加,因此永远不可能相同。
需要idx来区分此vue组件内的元素(它们都在同一个vue组件内,因此具有相同的_uid)