vuex-action

Utilities for vuex to create actions.
1,989 26
Install
npm install vuex-action
Latest Version:1.2.1
Vue:2
Older releases:1
License:MIT
Last Updated:Mar 14, 2017
Links: GitHub  ·  npm
Maintainer: varharrie

vuex-action

Utilities for vuex to easily create and manage actions.

  • Allow you to create untyped action
  • Support for Promise
  • Work with vue@1 and vue@2

Installation

  npm install --save vuex-action

API

  import {createAction, createActions} from 'vuex-action'

createAction(type?: string, payloadHandler?: () => any | Promise)

It creates an action, and the action type will generated by uuidV4() if not specified.

createActions(prefix?: string, payloadHandlers: Array | Object)

Similarly, creates a lot of actions.

Usage

For complete examples, see examples

  // Create an action
  const increment = createAction('ACTION_TYPE')
  // Or
  const increment = createAction()

With normal function:

  // PayloadHandler allows you to customize the payload
  const add = createAction((num) => num || 1)
  // Therefore
  store.dispatch('add') // + 1
  store.dispatch('add', 5) // + 5

With Promise:

  // Here is a function to fetch a user
  const fetchUserApi = function (name) {
    return Promise.resolve({username: name})
  }
  // Return a Promise
  const fetchUser = createAction((name) => fetchUserApi(name))
  store.dispatch('fetchUser', 'Harrie') // payload = {username: 'Harrie'}

Or create actions together:

// use `createActions` instance of `createAction`
const actions = createActions([
  'increment',
  {
    add: (num) => num || 1,
    fetchUser: (name) => fetchUserApi(name)
  }
])

The store:

  const store = new Vuex.Store({
    state: {
      count: 0,
      user: null
    },
    mutations: {
      // Just make it as a type
      [increment] (state, num) {
        state.count += num
      },
      [fetchUser] (state, user) {
        state.user = user
      }
    },
    actions: {
      increment,
      fetchUser
    }
  })

Inspired by

Related Packages

@wmde/vuex-helpers

Helper functions to ease working with Vuex, Centralized State Management for Vue...

8,736 4
vuex-alt

An alternative approach to Vuex helpers for accessing state, getters and actions...

1,976 14
spyfu-vuex-helpers

Utility functions for Vuex.

2,459 9
vuex-dry

DRY vuex, no more repetitive codes

2,136 51

Version History

Version Released Vue License
1.2.1 ^2.2.1 MIT
1.2.0 ^2.2.1 MIT
1.1.0 ^1.0.28 MIT
1.0.2 ^1.0.28 MIT
1.0.0 ^1.0.28 MIT
Pre-releases (3)
Version Released Vue License
1.2.0-beta.1 ^2.2.1 MIT
1.1.0-beta.4 ^1.0.28 MIT
1.1.0-beta.3 ^1.0.28 MIT