Subscribe to Vuex mutations
Using Yarn
$ yarn add vuex-subscriptions
Or NPM
$ npm install vuex-subscriptions
import addSubscriptions from 'vuex-subscriptions'
const modules = {
// Modules
}
const store = new Vuex.Store({
// ...
plugins: [
addSubscriptions({
subscriptions: {
// Options...
},
modules
})
],
modules
})
Creates a new instance of the plugin with the given options. The following options HAS TO be provided to configure the plugin for your specific needs:
subscriptions <Object>
: The keys are the mutations you want to listen for(including namespaces divided my forward-slash) and the value is the a callback function witch takes the state.modules <ModuleTree<any>>
: You could add the subscriptions inside your modules, and have it be namespaced.
If you have multiple callback functions for one mutation you just add the different functions in an array.
import { Store } from 'vuex'
import addSubscriptions from 'vuex-subscriptions'
const store = new Store({
// ...
plugins: [
addSubscriptions({
subscriptions: {
"user/loggedIn": (state, store) => {
// fetch profile info
},
"user/loggedOut": [
(state, store) => {
// delete some data
},
(state, store) => {
// navigate to another route
}
]
}
})
]
})
MIT © Andreas Storesund Madsen