Your solution seems to work and would not create unnecessary confusion for other developers (or yourself in six months). And even though Vuex seems overkill currently it may be beneficial later.
One thing which I am unsure about is how the modal is closed. I assume this is handled by the component displaying the image, by setting the value in the Vuex store to an empty string or NULL.
In case you want to avoid using Vuex you could use a simple event bus implementing a publish/subscribe pattern. This can be built using Vue itself. An example is:
const EventBus = new Vue();
This can be attached to the window
object or stored in a separate file and included the same way as components are. You can then a mounted
hook/method of your Vue component which handles the modal do:
Vue.component('modal-component-name', {
...
mounted() {
EventBus.$on('event-name', () => { ... });
}
});
This will listen for events with the name: 'event-name'. Then to trigger the event do the following in another component:
Vue.component('component-which-trigger-modal', {
...
methods: {
openModal() {
EventBus.$emit('event-name');
}
}
});
This also supports providing arguments when the event is triggered.
Keep in mind the event callback is not removed when the components is destroyed. To ensure multiple listener callbacks doing the same thing isn't attached when a component is loaded again use the Vue.$off() method when the component is destroyed. This can be done by implementing the destroyed
method on the component instance, which is called just before the component is destroyed. This requires you keep a reference to the callback used, so you cannot pass an anonymous method like I have done in the example.
A simple tutorial for the event bus can be found here: Creating a Global Event Bus with Vue.js.