通过VueJS中的道具将动态数据传递到引导程序的模态

麦克·塞伦斯基

我正在做一个小型个人项目,而我的引导模版有一个小问题。我有一个项目列表(卡片中的每个人),并且我想在每次单击卡片时显示详细信息。但是,无论我单击哪张卡,总是会在模态中得到相同的值(来自第一个项目)。这是我的代码

查看应用

<template>
  <div>
    <Navbar />
    <Projects />
  </div>
</template>
 
<script>
import Navbar from "./components/Navbar.vue";
import Projects from "./components/projects/Projects.vue";
 
export default {
  name: "App",
  components: { Navbar, Projects },
};
</script>

Projects.vue

<template>
  <div class="projects bg-light" id="projects_title">
    <h1>Projects</h1>
    <div class="projects_cards">
      <div v-for="project in projects" class="single_project" :key="project.id">
        <SingleProject :project="project"/>
          <Modal :project="project" />
      </div>
    </div>
  </div>
</template>
 
<script>
import SingleProject from "./SingleProject.vue";
import Modal from "../Modal.vue";
export default {
  data() {
    return {
      projects: [
        {
          id: "1",
          number: "III",
          title:
            "Title 4",
        },
        {
          id: "2",
          number: "IV",
          title: "Title 4",
        },
      ],
    };
  },
  components: {
    SingleProject,
    Modal,
  },
};
</script>

SingleProject.vue

<template>
  <div class="card mx-2" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">  {‌{project.number}} </h5>
    <h6 class="card-subtitle mb-2 text-muted">{‌{project.title}}</h6>
    <p class="card-text">Some quick example text</p>
    <a class="card-link" data-toggle="modal" data-target="#exampleModal">Project Card</a>
  </div>
</div>
</template>
 
<script>
export default {
  props: {project : {
    type: Object
  }},
}
</script>

模态

<template>
  <div>
    <div
      class="modal fade"
      id="exampleModal"
      tabindex="-1"
      aria-labelledby="exampleModalLabel"
      aria-hidden="true"
    >
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">{‌{project.id}}</h5>
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            {‌{project.title}}
          </div>
          <div class="modal-footer">
            <button
              type="button"
              class="btn btn-secondary"
              data-dismiss="modal"
            >
              Close
            </button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
export default {
  props: {
    project: {
      type: Object,
    },
  },
};
</script>

在道具内部,所有接缝都可以。 单击第二个项目后的模态

达里奥·雷加(DarioRega)

对我来说,这是问题所在:

<a class="card-link" data-toggle="modal" data-target="#exampleModal">Project Card</a>

数据目标触发程序#exampleModal是一个ID,并且所有模态都具有相同的ID,因此您将获得类似这样的奇怪行为。

您有不同的处理方式,例如使用@click事件会更好,但是您需要进行很多重构。

一个小的修复可能是这样的:

模态

:id="'exampleModal-' + project.id"

单项目

<a class="card-link" data-toggle="modal" :data-target="'#exampleModal-' + project.id">Project Card</a>

当然,请使用格式值更新所有ID。

让我知道进展如何

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章