Retrieve an event when a specific state state changes in vuex repository

I have a vuex repository with the following state:

state: { authed: false ,id: false } 

Inside the component, I want to see the changes in authed state and send an AJAX call to the server. This must be done in the various components.

I tried using store.watch() , but it fires when the id or authed value changes. I also noticed that it differs from vm.$watch in that you cannot specify a property. When I tried to do this:

 store.watch('authed',function(newValue,oldValue){ //some code }); 

I got this error:

[vuex] store.watch only accepts a function.

Any help is appreciated!

+5
source share
2 answers

Just set the getter for the authed state in your component and see what the local getter is:

 watch: { 'authed': function () { ... } } 
+13
source

Or you can use ...

 let suscribe = store.subscribe((mutation, state) => { console.log(mutation.type) console.log(mutation.payload) }) // call suscribe() for unsuscribe 

https://vuex.vuejs.org/en/api.html

+4
source

Source: https://habr.com/ru/post/1257647/


All Articles