Coder Social home page Coder Social logo

vuex-class.js's Introduction

此项目除了正常的bug修复,不再进行功能更新

如果对状态管理感兴趣,可以看下 Tms,文档更齐全

vuex-class.js

Coverage Status Build Status npm npm npm

Use es6 class to write the vuex module, making the code easier to maintain and expand.

Installing

npm install vuex-class.js --save

Document

Example

import Vue from 'vue'
import Vuex from 'vuex'
import VuexClass from 'vuex-class.js'

Vue
  .use(VuexClass)
  .use(Vuex)

class One extends VuexClass {
  constructor () {
    super()
    this.strict = process.env.NODE_ENV !== 'production'
    this.state = {
      count: 0
    }
    // Note: the sub module has no plugins option
    this.plugins = [
      VuexClass.init()
    ]
    this.modules = {
      two: new Two()
    }
  }
  // mutations
  set setCount (count) {
    this.state.count = count
  }
  // getters
  get countText () {
    return `text:${this.state.count}`
  }
  // actions
  clickCount () {
    setTimeout(() => {
      // Two methods to submit mutation
      // 1、Direct assignment
      // this.setCount = 1000
      // 2、Call method, note: if there is a get setCount attribute on class, this method does not exist.
      // this.setCount(1000)
    })
  }
}

class Two extends VuexClass {
  constructor () {
    super()
    this.state = {
      isBtn: false
    }
    this.modules = {
      three: new Three()
    }
    this.namespaced = true
  }
  set switchBtn (payload) {
    this.state.isBtn = !this.state.isBtn
  }
  get text () {
    return this.state.isBtn ? 'true' : 'false'
  }
}

class Three extends VuexClass {
  constructor () {
    super()
    this.state = {}
    this.namespaced = true
    // ...
  }
}

const one = new One()
const store = new Vuex.Store(one)

console.log(one.countText) // 'text:0'
console.log(one.modules.two.text) // 'false'

one.setCount = 666
one.modules.two.switchBtn()
console.log(one.countText) // 'text:666'
console.log(one.modules.two.text) // 'true'

const vm = new Vue({
  store,
  vuexClass: one,
  mapVuexClasses: { // Join the class in the component
    one: '',
    two: 'two',
    three: 'two/three'
  }
})

console.log(vm.one === one) // true
console.log(vm.two === one.modules.two) // true
console.log(vm.three === one.modules.two.modules.three) // true

API

  • VuexClass.init()
    When the store instance is created, the call

      const store = new Vuex.Store({
        // ...
        plugins: [
          VuexClass.init()
        ]
      })
  • VuexClass.bindClass(new Vuex.Store())
    When replacing the root state of store, we need to re bind class.

      const store = new Vuex.Store({
        // ...
        state: {
          // ...
        },
        plugins: [
          VuexClass.init()
        ]
      })
      store.replaceState({
        // ...
      })
    store.replaceState()
    VuexClass.bindClass(store)
  • VuexClass.mapVuexClasses(new VuexClass(), { ... })
    The 0.0.6 version is added to find the module and its sub modules, and return the relevant class.

      const classes = VuexClass.mapVuexClasses(one, {
        one: '',
        two: 'two',
        three: 'two/three'
      })
    
      console.log(classes.one === one) // true
      console.log(classes.two === one.modules.two) // true
      console.log(classes.three === one.modules.two.modules.three) // true

License

MIT

vuex-class.js's People

Contributors

lzxb avatar

Stargazers

李程 avatar  avatar  avatar  avatar Stanislav avatar  avatar  avatar Rockwell Schrock avatar  avatar myron avatar Rmaiy avatar laomu avatar  avatar Petar Slovic avatar archiewx avatar  avatar 王超 avatar faraway avatar kuangKim avatar  avatar zhangrongliang avatar  avatar  avatar 隔壁叔叔 avatar Winglau14 avatar 之梦 avatar Jeremy_Lvjr avatar  avatar

Watchers

James Cloos avatar Stanislav avatar  avatar  avatar  avatar

Forkers

a158abc ladyia

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.