Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
VueJS2 - Triggering modal component from another component

So this one is a bit tricky for me. I have a Sidebar.vue component with a link(Help). I want to trigger my Modal.vue component with different data for every main view eg. Home, About, Contact etc. So whenever I am on Home I want to trigger help modal with hints via that link for Home.vue, when I am on About I want to show hints for About.vue and so on..

My code:

Sidebar.vue

<template>
  <ul>
   <li>
    <a @click="openHelp">Help</a>
   </li>
 </ul>
</template>
<script>
export default {
    name: 'Sidebar',
    methods: {
        openHelp() {
            this.$emit('help-modal');
        },
    },
};
</script>

Modal.Vue

<template>
    <div class="modal" v-if="open">
        <div class="modal-title">
            <h4>Help</h4>
        </div>
    </div>
</template>

<script>

export default {
    name: 'Modal',

    props: {
        open: {
            type: Boolean,
            default: false,
        }
    },

    methods: {
        close() {
            this.$emit('closed');
        },
    },
};
</script>

Home.vue

<template>
  <div>
    Home
    <modal
      :open="helpOpen"
      @closed="openHelpModal">
       <p>Home Help</p>
        </modal>
  </div>
  <template>
    <sidebar/>
  </template>
</template>
<script>
import Sidebar from '@/components/Sidebar.vue';
export default {
  name: 'Home',
  components: {
    Sidebar,
  },
 data() {
   return {
     helpOpen: false,
  }
 }
 methods: {
   openHelpModal() {
     this.helpOpen = !this.helpOpen;
   },
 }
}
</script>

I know that vuex would be the best solution but don't have an idea how to approach it. Modal would show only static images with a bit of text for every main view.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

One way of doing this would be to simply add a property to the modal component e.g helpData and pass the relevant data to this prop in each of the main pages as shown below

Modal.vue

<template>
    <div class="modal" v-if="open">
        <div class="modal-title">
            <h4>Help</h4>
        </div>
    </div>
</template>

<script>

export default {
    name: 'Modal',

    props: {
        open: {
            type: Boolean,
            default: false,
        },
        helpData: {
            type: String,
            required: true
        }
    },

    methods: {
        close() {
            this.$emit('closed');
        },
    },
};
</script>

Home.vue

<template>
  <div>
    Home
    <modal
      :open="helpOpen"
      @closed="openHelpModal"
      :help-data="This is a sample help data for the home view"
    />
  </div>
  <template>
    <sidebar @help-modal="helpOpen = true"/>
  </template>
</template>
<script>
import Sidebar from '@/components/Sidebar.vue';
export default {
  name: 'Home',
  components: {
    Sidebar,
  },
 data() {
   return {
     helpOpen: false,
  }
 }
 methods: {
   openHelpModal() {
     this.helpOpen = !this.helpOpen;
   },
 }
}
</script>

As indicated by @ljubadr, I have included the logic that would open the modal in the sidebar component of Home.vue. Also, I would recommend you change the name of the function openHelpModal to closeHelpModal (seeing as this function will basically close the modal) or toggleHelpModal (since from the logic of the function, it toggles the modal state).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda