简体   繁体   中英

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')
  }
)

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')
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM