简体   繁体   English

从循环到 Vue 子组件的 Vue 父组件传递动态数据

[英]Passing dynamic data from a Vue parent component that loops to a Vue child component

I'm using v-bind to pass data from a parent Vue component (Project) that is looping to a child component: a button that opens a modal to edit the project.我正在使用 v-bind 将数据从循环到子组件的父 Vue 组件(项目)传递:一个打开模式以编辑项目的按钮。 I'm using "props" in my child component to register the data.我在我的子组件中使用“道具”来注册数据。 Problem: only the data from the first Project is passed, and is repeated to the next tasks.问题:只有第一个项目的数据被传递,并重复到下一个任务。 What I am doing wrong?我做错了什么?

ProjectComponent (Parent):项目组件(父):

 <div class="card my-2" v-for="project in projects" :key="project.id">
    <div class="card-header">{{ project.name }}</div>
    <div class="card-body">{{ project.description }}
       <edit-project :item="project"></edit-project>
    </div>
 </div>

EditProjectComponent (Child): EditProjectComponent(子):

<template>
<div class="mt-2">
    <!-- Button trigger modal -->
    <button
        type="button"
        class="btn btn-secondary btn-sm"
        data-toggle="modal"
        data-target="#editModal"
    >
        +
    </button>

    <!-- Modal -->
    <div
        class="modal fade"
        id="editModal"
        tabindex="-1"
        role="dialog"
        aria-labelledby="editModalLabel"
        aria-hidden="true"
    >
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editModalLabel">
                        {{ item.name }}
                    </h5>
                    <button
                        type="button"
                        class="close"
                        data-dismiss="modal"
                        aria-label="Close"
                    >
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="name">Nom du projet</label>
                            <input
                                type="text"
                                id="name"
                                name="name"
                                class="form-control"
                                v-model="item.name"
                            />
                        </div>
                        <div class="form-group">
                            <label for="description">Description</label>
                            <textarea
                                type="text"
                                id="description"
                                name="description"
                                class="form-control"
                                v-model="item.description"
                            >
                            ></textarea
                            >
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button
                        type="button"
                        class="btn btn-secondary btn-sm"
                        data-dismiss="modal"
                    >
                        Fermer
                    </button>
                    <button
                        type="button"
                        class="btn btn-primary btn-sm"
                        data-dismiss="modal"
                    >
                        Enregistrer
                       </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: "EditProject",
    props: ["item"],
};
</script>

And this an example of the array:这是数组的一个例子:

 [
{
"id": 8,
"name": "rodriguez.com",
"description": "Vero ut inventore ex omnis quibusdam. Quam nobis laboriosam quae optio. Explicabo sed incidunt quia dolores maiores.",
"created_at": "2020-04-05T09:18:37.000000Z",
"updated_at": "2020-04-05T09:18:37.000000Z"
},
{
"id": 19,
"name": "Tyler Matthews",
"description": "Veritatis dolorum do",
"created_at": "2020-04-05T14:52:02.000000Z",
"updated_at": "2020-04-05T14:52:02.000000Z"
},
{
"id": 21,
"name": "Sacha Leblanc",
"description": "Recusandae Fugit d",
"created_at": "2020-04-05T19:40:36.000000Z",
"updated_at": "2020-04-05T19:40:36.000000Z"
}
]

So I finally found the solution.所以我终于找到了解决方案。 The problem comes from reusing the same bootstrap modal... Not the Vue code... So dumb: The solution is described in the bootstrap documentation: https://getbootstrap.com/docs/4.0/components/modal/#varying-modal-content问题来自重用相同的引导模式......不是Vue代码......太愚蠢了:引导文档中描述了解决方案: https://getbootstrap.com/docs/4.0/components/modal/#variing-模态内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM