Shubham Chaudhary
Shubham Chaudhary

Reputation: 1482

How to watch for state change in Vuex properly?

I'm using below code to watch for the change in Vuex state. The store file lies in the same directory. However, it doesn't seem to work. How do I watch for state change in vuex properly?

import store from './index'
store.watch(
  (store) => store.state.base_url,
  () => {
    console.log('Watcher works')
  }
)

Upvotes: 2

Views: 4513

Answers (1)

Decade Moon
Decade Moon

Reputation: 34306

Are you sure you're just not using watch correctly? According to the docs, the watch function receives the store's state as the first argument, so it should be:

store.watch(
  state => state.base_url,
  () => console.log('Watcher works')
)

Upvotes: 3

Related Questions