Coder Social home page Coder Social logo

ejfrancis / vuex-alt Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 2.0 73 KB

An alternative approach to Vuex helpers for accessing state, getters and actions that doesn't rely on string constants.

License: MIT License

JavaScript 100.00%
vue vuex flux-architecture flux state-management

vuex-alt's Introduction

Vuex-Alt

An alternative, opinionated approach to Vuex helpers for accessing state, getters and actions that doesn't rely on string constants.

Installation

First install the npm package with

npm install --save vuex-alt

Then use the plugin, passing in the Vuex Store.

import Vuex from 'vuex';
import { VuexAltPlugin } from 'vuex-alt';

// use Vuex as usual
Vue.use(Vuex);

// create your store
const store = new Vuex.Store({ ... });

// use the VuexAltPlugin, and pass it
// the new Vuex Store
Vue.use(VuexAltPlugin, { store });

Prerequisites

Vuex-Alt makes two intentional, opinionated assumptions about your Vuex code:

  1. Mutations are only commited from within actions. Components never directly commit mutations. Every mutation has an accompanying action.
  2. All Vuex state, getters and actions are organized into Vuex modules.

These two rules lead to more scalable state management code, and more predictable state changes.

API Usage

Vuex-Alt provides an alternative approach to the Vuex helpers for mapState, mapActions, and mapGetters.

The main difference between the Vuex-Alt helpers and the original Vuex helpers is that instead of accepting strings to specify the namespace and action/getter you want, access is done via functions and nested objects.

mapState()

Provide an object that maps local Vuex instance properties to Vuex module properties.

For example, if you have a state property called count on a Vuex store module called counter you would access it like this:

computed: {
  ...mapState({
    count: (state) => state.counter.count
  })
}

mapActions()

Provide an object that maps local Vuex instance methods to Vuex module methods.

For example, if you have an action called increment() on a Vuex store module called counter you would access it like this:

methods: {
  ...mapActions({
    increment: (actions) => actions.counter.increment
  })
}

Now you can access it in your component via this.increment(10).

mapGetters()

Provide an object that maps local Vuex instance properties to Vuex module getters.

For example, if you have a getter called countPlusTen() on a Vuex store module called counter you would access it like this:

computed: {
  ...mapGetters({
    countPlusTen: (getters) => getters.counter.countPlusTen
  })
}

vuex-alt's People

Contributors

ejfrancis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vuex-alt's Issues

Typescript support?

It would be amazing if this module supported typing, as it would lend even more usability to the "non-string-based" vuex approach you've implemented here. Do you have any interest in that? I may be willing to pitch in if it's something you're interested in pulling in.

IE11 problems?

I think this package is awesome!

We are using it in all our projects. But one things we saw is that our app does not work on IE11, and apparently the reason are that we have arrow functions that are not being transpiled, and this arrow functions seem to be from this package. Would you know if that's plausible?

Thanks

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.