0.4.0 • Published 5 years ago

vue-light-store v0.4.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

vue-light-store

A simple Vue store.

Usage

See file examples/index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="../dist/VueLightStore.js"></script>
</head>

<body>
<div id="app">
  <p>The plugin will inject two properties into vue instance: <code>$store</code> and <code>$state</code>.</p>
  <p>Use <code>$state.MODULE.SOME_STATE</code> to access the state.</p>
  <p>Use <code>$store.MODULE.METHOD()</code> to call the method.</p>

  <p>$state.moduleFoo.a: {{$state.moduleFoo.a}}; $state.moduleFoo.b: {{$state.moduleFoo.b}} <button @click="$store.moduleFoo.inc">$store.moduleFoo.inc()</button></p>
  <p>$state.moduleBar.c: {{$state.moduleBar.c}} <button @click="$store.moduleBar.inc">$store.moduleBar.inc()</button></p>
  <p><button @click="reset">reset</button></p>
</div>

<script>
Vue.use(VueLightStore)

const store = new VueLightStore({
  // define some modules
  moduleFoo: {
    state: () => ({
      a: 1,
      b: 2
    }),

    methods: {
      inc() {
        // this refers to the module
        // access state via this.$state
        this.$state.a++

        // access other methods
        this.incB()
      },

      incB() {
        this.$state.b++
      }
    }
  },

  moduleBar: {
    state: () => ({
      c: 3
    }),

    methods: {
      inc() {
        this.$state.c++
      }
    }
  }
})

new Vue({
  el: '#app',
  store,

  methods: {
    reset() {
      // store.$setState(state) will merge the state into store.$state
      this.$store.$setState({
        moduleFoo: {
          a: 0,
          b: 1
        },

        moduleBar: {
          c: 0
        }
      })
    }
  }
})
</script>
</body>
</html>

License

MIT

0.4.0

5 years ago

0.3.0

6 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.1.0-dev.1

7 years ago

0.1.0-dev.0

7 years ago