Skip to content
Advertisement

Observe to some event from another component in Vue

I have two components:

<div>
    <advertiser-add-component></advertiser-add-component>
    <advertisers-component></advertisers-component>
</div>

These components have the following interface: enter image description here

First component is a button. I will see a modal dialog when I click on it. The modal contains an Ajax form:

enter image description here

Second component is a table. You already saw it on the first picture So, I would like to rerender the table component after sending the form without page refreshing. Is there some way to “subscribe” to this event (once the modal information is sent) from advertisers-component? How I can organize it with minimum code?

Advertisement

Answer

Yes absolutely, for something like this you could spin up a simple event bus, your component would emit an update to the bus, and the other watches for updates to that event type on the bus.

Here’s a good example of how to do it:

https://alligator.io/vuejs/global-event-bus/

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