Skip to content

asmadsen/vuex-subscriptions

Repository files navigation

vuex-subscriptions

Subscribe to Vuex mutations

npm

Requirements

Installation

Using Yarn

$ yarn add vuex-subscriptions

Or NPM

$ npm install vuex-subscriptions

Usage

import addSubscriptions from 'vuex-subscriptions'

const modules = {
	// Modules
}

const store = new Vuex.Store({
  // ...
  plugins: [
  	addSubscriptions({
  		subscriptions: {
  			// Options...
  		},
  		modules
  	})
  ],
  modules
})

API

addSubscriptions([options])

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.

Example

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

License

MIT © Andreas Storesund Madsen

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published