Search code examples
vue.jsvuex

How do I run a function on a different component with vuex (non parent/child components)


I have 2 non parent/child components. I'd like to run a function on component A by clicking a button on component B. I know that the eventbus is a solution but because I am already using Vuex I would like to do it the Vuex way but could not find an example on the internet.


Solution

  • Have component A mutate some state that component B is watching. When component B see's a change to that state, run your function.

    The mutation could be as simple as flipping a boolean or incrementing a number. Anything to trigger the watch in the other component.

    Here is how to setup the watch: https://stackoverflow.com/a/44347195/1409310