Categories
Vue Answers

How to watch store values from Vuex?

Spread the love

Sometimes, we want to watch store values from Vuex.

In this article, we’ll look at how to watch store values from Vuex.

How to watch store values from Vuex?

To watch store values from Vuex, we can add a watcher in our component to watch the store value.

For instance, we write

<script>
//...
export default {
  watch: {
    "$store.state.drawer"(val) {
      console.log(val);
    },
  },
};
</script>

to watch the $store.state.drawer value in our component by setting '$store.state.drawer' to a function with the val parameter.

We get the latest value of the $store.state.drawer property from the val parameter.

Conclusion

To watch store values from Vuex, we can add a watcher in our component to watch the store value.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *