Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!