API Reference | Vuex (original) (raw)

Store

createStore

import { createStore } from 'vuex'  
const store = createStore({ ...options })  

Store Constructor Options

state

mutations

actions

{  
  state,      // same as `store.state`, or local state if in modules  
  rootState,  // same as `store.state`, only in modules  
  commit,     // same as `store.commit`  
  dispatch,   // same as `store.dispatch`  
  getters,    // same as `store.getters`, or local getters if in modules  
  rootGetters // same as `store.getters`, only in modules  
}  

And also receives a second payload argument if there is one.
Details

getters

state,     // will be module local state if defined in a module.  
getters    // same as store.getters  

Specific when defined in a module

state,       // will be module local state if defined in a module.  
getters,     // module local getters of the current module  
rootState,   // global state  
rootGetters  // all getters  

Registered getters are exposed on store.getters.
Details

modules

{  
  key: {  
    state,  
    namespaced?,  
    mutations?,  
    actions?,  
    getters?,  
    modules?  
  },  
  ...  
}  

Each module can contain state and mutations similar to the root options. A module's state will be attached to the store's root state using the module's key. A module's mutations and getters will only receives the module's local state as the first argument instead of the root state, and module actions' context.state will also point to the local state.
Details

plugins

strict

devtools

Store Instance Properties

state

getters

Store Instance Methods

commit

Commit a mutation. options can have root: true that allows to commit root mutations in namespaced modules. Details

dispatch

Dispatch an action. options can have root: true that allows to dispatch root actions in namespaced modules. Returns a Promise that resolves all triggered action handlers. Details

replaceState

Replace the store's root state. Use this only for state hydration / time-travel purposes.

watch

Reactively watch fn's return value, and call the callback when the value changes. fn receives the store's state as the first argument, and getters as the second argument. Accepts an optional options object that takes the same options as Vue's vm.$watch method.

To stop watching, call the returned unwatch function.

subscribe

Subscribe to store mutations. The handler is called after every mutation and receives the mutation descriptor and post-mutation state as arguments.

const unsubscribe = store.subscribe((mutation, state) => {
  console.log(mutation.type)
  console.log(mutation.payload)
})

// you may call unsubscribe to stop the subscription
unsubscribe()

By default, new handler is added to the end of the chain, so it will be executed after other handlers that were added before. This can be overridden by adding prepend: true to options, which will add the handler to the beginning of the chain.

store.subscribe(handler, { prepend: true })

The subscribe method will return an unsubscribe function, which should be called when the subscription is no longer needed. For example, you might subscribe to a Vuex Module and unsubscribe when you unregister the module. Or you might call subscribe from inside a Vue Component and then destroy the component later. In these cases, you should remember to unsubscribe the subscription manually.

Most commonly used in plugins. Details

subscribeAction

Subscribe to store actions. The handler is called for every dispatched action and receives the action descriptor and current store state as arguments. The subscribe method will return an unsubscribe function, which should be called when the subscription is no longer needed. For example, when unregistering a Vuex module or before destroying a Vue component.

const unsubscribe = store.subscribeAction((action, state) => {
  console.log(action.type)
  console.log(action.payload)
})

// you may call unsubscribe to stop the subscription
unsubscribe()

By default, new handler is added to the end of the chain, so it will be executed after other handlers that were added before. This can be overridden by adding prepend: true to options, which will add the handler to the beginning of the chain.

store.subscribeAction(handler, { prepend: true })

The subscribeAction method will return an unsubscribe function, which should be called when the subscription is no longer needed. For example, you might subscribe to a Vuex Module and unsubscribe when you unregister the module. Or you might call subscribeAction from inside a Vue Component and then destroy the component later. In these cases, you should remember to unsubscribe the subscription manually.

subscribeAction can also specify whether the subscribe handler should be called before or after an action dispatch (the default behavior is before):

store.subscribeAction({
  before: (action, state) => {
    console.log(`before action ${action.type}`)
  },
  after: (action, state) => {
    console.log(`after action ${action.type}`)
  }
})

subscribeAction can also specify an error handler to catch an error thrown when an action is dispatched. The function will receive an error object as the third argument.

store.subscribeAction({
  error: (action, state, error) => {
    console.log(`error action ${action.type}`)
    console.error(error)
  }
})

The subscribeAction method is most commonly used in plugins. Details

registerModule

Register a dynamic module. Details

options can have preserveState: true that allows to preserve the previous state. Useful with Server Side Rendering.

unregisterModule

Unregister a dynamic module. Details

hasModule

hotUpdate

Hot swap new actions and mutations. Details

Component Binding Helpers

mapState

Create component computed options that return the sub tree of the Vuex store. Details

The first argument can optionally be a namespace string. Details

The second object argument's members can be a function. function(state: any)

mapGetters

Create component computed options that return the evaluated value of a getter. Details

The first argument can optionally be a namespace string. Details

mapActions

Create component methods options that dispatch an action. Details

The first argument can optionally be a namespace string. Details

The second object argument's members can be a function. function(dispatch: function, ...args: any[])

mapMutations

Create component methods options that commit a mutation. Details

The first argument can optionally be a namespace string. Details

The second object argument's members can be a function. function(commit: function, ...args: any[])

createNamespacedHelpers

Create namespaced component binding helpers. The returned object contains mapState, mapGetters, mapActions and mapMutations that are bound with the given namespace. Details

Composable Functions

useStore

import { useStore } from 'vuex'  
export default {  
  setup () {  
    const store = useStore()  
  }  
}  

TypeScript users can use an injection key to retrieve a typed store. In order for this to work, you must define the injection key and pass it along with the store when installing the store instance to the Vue app.
First, declare the injection key using Vue's InjectionKey interface.

// store.ts  
import { InjectionKey } from 'vue'  
import { createStore, Store } from 'vuex'  
export interface State {  
  count: number  
}  
export const key: InjectionKey<Store<State>> = Symbol()  
export const store = createStore<State>({  
  state: {  
    count: 0  
  }  
})  

Then, pass the defined key as the second argument for the app.use method.

// main.ts  
import { createApp } from 'vue'  
import { store, key } from './store'  
const app = createApp({ ... })  
app.use(store, key)  
app.mount('#app')  

Finally, you can pass the key to the useStore method to retrieve the typed store instance.

// in a vue component  
import { useStore } from 'vuex'  
import { key } from './store'  
export default {  
  setup () {  
    const store = useStore(key)  
    store.state.count // typed as number  
  }  
}