GithubHelp home page GithubHelp logo

kenberkeley / vue-state-management-alternative Goto Github PK

View Code? Open in Web Editor NEW
70.0 7.0 7.0 29 KB

Vuex state management alternative for both Vue 1.x & 2.x

Vue 69.24% JavaScript 30.76%
vue state-management alternative vuex

vue-state-management-alternative's Introduction

Alternative state management for Vue

Chinese README - 中文说明

.sync is deprecated in Vue 2.x (UPDATE: a castration edition is back in v2.3)
Especially for the case that props passed shallowly, you have to write more verbose code
The docs mentioned using v-model as a hack which sounds hardly elegant
Also, the official solution Vuex is not suitable for independent components
We all know one-way data flow is a best practice
but development efficiency, maintainability, simple and straight-forward should all be taken into consideration

After reviewing the below contents of the docs:

We can implement an awesome state management solution ourselves

Firstly, Take a look at src/sharedState.mixin.js:

// The magic of state persistance is closure here
export const sharedState = {
  todos: [],
  counter: 0
}

export const resetTodos = () => sharedState.todos = []
export const resetCounter = () => sharedState.counter = 0
export const addTodo = (s) => sharedState.todos.push(s)
export const inc = () => sharedState.counter++
export const dec = () => sharedState.counter--

/**
 * @exports.default {Mixin}
 */
export default {
  data: () => ({
    sharedState
  }),
  methods: {
    resetTodos,
    resetCounter,
    addTodo,
    inc,
    dec
  }
}

/*

  You can access the shared state and methods
  not only within Vue component (as mixins) but also in common js files

  ※ e.g.
  import { sharedState, inc } from '<path to>/sharedState.mixin'
  
  console.info(sharedState.counter) // 0
  inc()
  console.info(sharedState.counter) // 1
 
 */

Then, apply to the component tree below:

App
 ├─ Child1
 |    └─ Grandchild1
 └─ Child2
      └─ Grandchild2

Finally we get an online demo

It's highly suggested to dive into src/ to learn about how to use it
This solution is applicable for Vue 1.x / 2.x


Test it yourself

$ git clone https://github.com/kenberkeley/vue-state-management-alternative.git
$ cd vue-state-management-alternative
$ npm i
$ npm start

vue-state-management-alternative's People

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-state-management-alternative's Issues

又来问个问题

发现这个只能做单页面内的状态维护,想做全局的状态维护怎做呢?感谢回答!

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.