L
L
Lecturer2022-02-14 14:41:13
JavaScript
Lecturer, 2022-02-14 14:41:13

How to make a dynamic modal in vue?

I just started to study vue and now there is such a task, there is an array with data, these are projects

data() {
            return {
                projects: [
                    {
                        id: '1',
                        src: 'siemens.jpg',
                        title: 'Siemens'

                    },
                    {
                        id: '2',
                        src: 'nokia.jpg',
                        title: 'Nokia'
                    },
         ..............

and I output them via v-for:
<div v-for="item in projects" :key="item.id" @click="showModal">
     <img :src="require('@/assets/portfolio/' + item.src)" :alt="item.title" loading="lazy" width="200" height="200">
      <p>{{item.title}}</p>
</div>

I want to make it so that when you click on the project, a modal window opens and the project data (name, picture) is transferred there. So that there is one Modal.vue component and somehow transfer data there, because there can be many projects and you don’t want to make your own modal for each project. Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2022-02-14
@Rsa97

If this is only for one page, then you can do without a component. Just add a modal div to the page that will show up when you select a project. As long as selectedProject has a value cast to false (eg null) - that div is hidden. We wrote down a link to the project there - the div was activated.

<div v-show="selectedProject" class="modal">
  ...
  {{ selectedProject.title }} 
  ...
</div>

S
Spaceoddity, 2022-02-14
@Spaceoddity

https://ru.vuejs.org/v2/examples/modal.html
And throw data into it through props.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question