项目作者: ktsn

项目描述 :
Component agnostic Vuex mappers
高级语言: TypeScript
项目地址: git://github.com/ktsn/vuex-mappers.git
创建时间: 2018-04-08T14:47:44Z
项目社区:https://github.com/ktsn/vuex-mappers

开源协议:MIT License

下载


vuex-mappers

Component agnostic Vuex mappers.

Usage

Install it via npm:

  1. $ npm install vuex-mappers

There are four mappers in vuex-mappers: state, getter, mutation and action. They have similar API with Vuex’s mapXXX helpers but are Vue component agnostic and return only one bound function.

For example, imagine following store:

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. export const store = new Vuex.Store({
  5. state: {
  6. count: 0
  7. },
  8. getters: {
  9. double: state => state.count * 2
  10. },
  11. mutations: {
  12. inc(state, amount) {
  13. state.count += amount
  14. }
  15. },
  16. actions: {
  17. incAsync({ commit }, amount) {
  18. setTimeout(() => {
  19. commit('inc', amount)
  20. }, 1000)
  21. }
  22. }
  23. })

Then you declare store mappers:

  1. import { state, getters, mutations, actions } from 'vuex-mappers'
  2. import { store } from './store'
  3. const count = state('count')(store)
  4. const double = getter('double')(store)
  5. const inc = mutation('inc')(store)
  6. const incAsync = action('incAsync')(store)
  7. console.log(count()) // store.state.count
  8. console.log(double()) // store.getters.double
  9. inc(123) // store.commit('inc', 123)
  10. incAsync(123) // store.dispatch('incAsync', 123)

You may want to pass namespace for the mapper:

  1. import Vuex from 'vuex'
  2. import { state } from 'vuex-mappers'
  3. const store = new Vuex.Store({
  4. modules: {
  5. foo: {
  6. namespaced: true,
  7. state: {
  8. message: 'Hello'
  9. }
  10. }
  11. }
  12. })
  13. // You can specify namespace value to 1st argument
  14. const message = state('foo/', 'message')(store)
  15. console.log(message()) // -> Hello

state mapper also receive a function which receives store state and getters:

  1. import Vuex from 'vuex'
  2. import { state } from 'vuex-mappers'
  3. const store = new Vuex.Store({
  4. state: {
  5. count: 1
  6. },
  7. getters: {
  8. double: state => state.count * 2
  9. }
  10. })
  11. const value = state((state, getters) => {
  12. return state.count + getters.double
  13. })(store)
  14. console.log(value()) // -> 3

mutation and action mapper can receive a function for more flexible mapping:

  1. import Vuex from 'vuex'
  2. import { mutation, action } from 'vuex-mappers'
  3. const store = new Vuex.Store({
  4. state: {
  5. count: 0
  6. },
  7. mutations: {
  8. inc(state, amount) {
  9. state.count += amount
  10. }
  11. },
  12. actions: {
  13. incAsync({ commit }, amount) {
  14. setTimeout(() => {
  15. commit('inc', amount)
  16. }, 1000)
  17. }
  18. }
  19. })
  20. const incDouble = mutation((commit, amount) => {
  21. commit('inc', amount * 2)
  22. })(store)
  23. const incDoubleAsync = action((dispatch, amount) => {
  24. dispatch('incAsync', amount * 2)
  25. })(store)
  26. incDouble(123)
  27. incDoubleAsync(123)

License

MIT