Giter Site home page Giter Site logo

alajfit / vuex-action-patcher Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 1.12 MB

๐Ÿ’šAdd tools to your Vuex action context

Home Page: https://alajfit.github.io/vuex-action-patcher/

License: MIT License

HTML 2.99% Vue 6.70% JavaScript 90.31%
vuex vuex-action vuex-plugin

vuex-action-patcher's Introduction

Vuex Action Patcher (Adding Tools to Your Context)

Build Status codecov npm npm

Add tools to your Vuex action context

Motivation

I found making tools available to the Vuex action context could be handy for numerous reasons. The library is designed for you to pass the same toolset to all actions from within the context.

  • add tools to all actions
  • add tools to actions within registered modules
  • keep consistent tooling across all actions

Install

npm install vuex-action-patcher --save

Usage

Use in a Vue App

import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'
import vuexActionPatcher from 'vuex-action-patcher'
import { name } from '../package.json'
import moment from 'moment'

Vue.use(Vuex)

const tools = vuexActionPatcher(Vuex, { moment })

const store = new Vuex.Store({
  state: {
    name
  },
  actions: {
    getTime ({ moment }) {
      console.log(moment().calendar())
    }
  },
  plugins: [tools]
})

new Vue({
  el: '#app',
  store,
  render: h => h(App)
})

Use with Dynamic Modules

main.js

import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'
import vuexActionPatcher from 'vuex-action-patcher'
import { name } from '../package.json'
import moment from 'moment'

Vue.use(Vuex)

const tools = vuexActionPatcher(Vuex, { moment })

const store = new Vuex.Store({
  plugins: [tools]
})

new Vue({
  el: '#app',
  store,
  render: h => h(App)
})

later.js

store.registerModule('myModule', {
  state: {
    name: 'myModule'
  },
  actions: {
    getTime ({ moment }) {
      console.log(moment().calendar())
    }
  }
})

Use in a Nuxt App

Under store/index.js

import Vuex from 'vuex'
import vuexActionPatcher from 'vuex-action-patcher'
import moment from 'moment'

const vuexActionPatch = vuexActionPatcher(Vuex, { moment })

const createStore = () => {
  return new Vuex.Store({
    state: () => ({
      counter: 0
    }),
    mutations: {
      increment (state) {
        state.counter++
      }
    },
    actions: {
        getDate ({ moment }) {
            console.log(moment().calendar())
        }
    },
    plugins: [vuexActionPatch]
  })
}

export default createStore

vuex-action-patcher's People

Contributors

alajfit avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.