I scaffolded a project from vue-cli
using the webpack
template.
Now in the App
component I created a bootstrap modal dialog, which I want to reuse from the entire application. Along with that I also created one method in the App
component called showMessage
which actually does the work of showing the modal.
Now considering that I should be able to reach that method from any component, is calling like what is shown below a bad idea?
this.$root.$children[0].showMessage('Message', `An email will be sent to ${this.form.email}`)
That is a fragile way to handle global messaging.
At least register an event on the root component from inside the modal component:
Then you can use
this.$root.$emit('showMessage', 'foo message')
to display a message from anywhere within the scope of that root component.Even better might be to create an event bus:
This way, you have a slightly better separation of concerns.