Skip to content
Advertisement

Open a Vuetify dialog from a component template in VueJS

I’m using the VueJS Vuetify framework and I need to open a dialog – that gets imported as a component template – from another template. Once the Menu button in App.vue got clicked, the Modal should open. Here is my setup:

  • App.vue = navigation template with Menu button
  • Modal.vue = Modal template, imported as global in main.js

main.js

JavaScript

Modal.vue Template:

JavaScript

How to open the dialog?

Advertisement

Answer

You can open the dialog using custom events and using an event bus for non parent-child communication.

If your application gets a bit more complex I recommend you use Vuex for state management.


Event bus solution:

In your main.js or in a new file create and export a new Vue instance :

export const bus = new Vue()

In app.vue import the busand emit the event:

JavaScript

In modal.vue also import the bus and listen for the event in the created hook:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement