API Reference

Store

createStore

  • createStore<S>(options: StoreOptions<S>): Store<S>

    Creates a new store.

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

Store Constructor Options

state

  • type: Object | Function

    The root state object for the Vuex store. Details

    If you pass a function that returns an object, the returned object is used as the root state. This is useful when you want to reuse the state object especially for module reuse. Details

mutations

  • type: { [type: string]: Function }

    Register mutations on the store. The handler function always receives state as the first argument (will be module local state if defined in a module), and receives a second payload argument if there is one.

    Details

actions

  • type: { [type: string]: Function }

    Register actions on the store. The handler function receives a context object that exposes the following properties:

    1. {
    2. state, // same as `store.state`, or local state if in modules
    3. rootState, // same as `store.state`, only in modules
    4. commit, // same as `store.commit`
    5. dispatch, // same as `store.dispatch`
    6. getters, // same as `store.getters`, or local getters if in modules
    7. rootGetters // same as `store.getters`, only in modules
    8. }

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

    Details

getters

  • type: { [key: string]: Function }

    Register getters on the store. The getter function receives the following arguments:

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

    Specific when defined in a module

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

    Registered getters are exposed on store.getters.

    Details

modules

  • type: Object

    An object containing sub modules to be merged into the store, in the shape of:

    1. {
    2. key: {
    3. state,
    4. namespaced?,
    5. mutations?,
    6. actions?,
    7. getters?,
    8. modules?
    9. },
    10. ...
    11. }

    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

  • type: Array<Function>

    An array of plugin functions to be applied to the store. The plugin simply receives the store as the only argument and can either listen to mutations (for outbound data persistence, logging, or debugging) or dispatch mutations (for inbound data e.g. websockets or observables).

    Details

strict

  • type: boolean

  • default: false

    Force the Vuex store into strict mode. In strict mode any mutations to Vuex state outside of mutation handlers will throw an Error.

    Details

devtools

  • type: boolean

    Turn the devtools on or off for a particular Vuex instance. For instance, passing false tells the Vuex store to not subscribe to devtools plugin. Useful when you have multiple stores on a single page.

    1. {
    2. devtools: false
    3. }

Store Instance Properties

state

  • type: Object

    The root state. Read only.

getters

  • type: Object

    Exposes registered getters. Read only.

Store Instance Methods

commit

  • commit(type: string, payload?: any, options?: Object)
  • commit(mutation: Object, options?: Object)

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

dispatch

  • dispatch(type: string, payload?: any, options?: Object): Promise<any>
  • dispatch(action: Object, options?: Object): Promise<any>

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

  • replaceState(state: Object)

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

watch

  • watch(fn: Function, callback: Function, options?: Object): Function

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(handler: Function, options?: Object): Function

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

  1. const unsubscribe = store.subscribe((mutation, state) => {
  2. console.log(mutation.type)
  3. console.log(mutation.payload)
  4. })
  5. // you may call unsubscribe to stop the subscription
  6. 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.

  1. 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

  • subscribeAction(handler: Function, options?: Object): Function

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.

  1. const unsubscribe = store.subscribeAction((action, state) => {
  2. console.log(action.type)
  3. console.log(action.payload)
  4. })
  5. // you may call unsubscribe to stop the subscription
  6. 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.

  1. 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):

  1. store.subscribeAction({
  2. before: (action, state) => {
  3. console.log(`before action ${action.type}`)
  4. },
  5. after: (action, state) => {
  6. console.log(`after action ${action.type}`)
  7. }
  8. })

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.

  1. store.subscribeAction({
  2. error: (action, state, error) => {
  3. console.log(`error action ${action.type}`)
  4. console.error(error)
  5. }
  6. })

The subscribeAction method is most commonly used in plugins. Details

registerModule

  • registerModule(path: string | Array<string>, module: Module, options?: Object)

Register a dynamic module. Details

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

unregisterModule

  • unregisterModule(path: string | Array<string>)

Unregister a dynamic module. Details

hasModule

  • hasModule(path: string | Array<string>): boolean

    Check if the module with the given name is already registered. Details

hotUpdate

  • hotUpdate(newOptions: Object)

Hot swap new actions and mutations. Details

Component Binding Helpers

mapState

  • mapState(namespace?: string, map: Array<string> | Object<string | function>): Object

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

  • mapGetters(namespace?: string, map: Array<string> | Object<string>): Object

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

The first argument can optionally be a namespace string. Details

mapActions

  • mapActions(namespace?: string, map: Array<string> | Object<string | function>): Object

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

  • mapMutations(namespace?: string, map: Array<string> | Object<string | function>): Object

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

  • createNamespacedHelpers(namespace: string): Object

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

  • useStore<S = any>(injectKey?: InjectionKey<Store<S>> | string): Store<S>;

    Fetches the injected store when called inside the setup hook. When using the Composition API, you can retrieve the store by calling this method.

    1. import { useStore } from 'vuex'
    2. export default {
    3. setup () {
    4. const store = useStore()
    5. }
    6. }

    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.

    1. // store.ts
    2. import { InjectionKey } from 'vue'
    3. import { createStore, Store } from 'vuex'
    4. export interface State {
    5. count: number
    6. }
    7. export const key: InjectionKey<Store<State>> = Symbol()
    8. export const store = createStore<State>({
    9. state: {
    10. count: 0
    11. }
    12. })

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

    1. // main.ts
    2. import { createApp } from 'vue'
    3. import { store, key } from './store'
    4. const app = createApp({ ... })
    5. app.use(store, key)
    6. app.mount('#app')

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

    1. // in a vue component
    2. import { useStore } from 'vuex'
    3. import { key } from './store'
    4. export default {
    5. setup () {
    6. const store = useStore(key)
    7. store.state.count // typed as number
    8. }
    9. }