Giter Club home page Giter Club logo

vuex-light's Introduction

vuex-light

npm npm bundle size GitHub Workflow Status (branch) npm downloads last commit

Demo

Features

  • Robust typescript support ๐Ÿ’ช
  • Consistent and intuitive interface ๐Ÿ’ก
  • Implement with vue 3 reactivity system only
  • Light weight

Installation

via npm

yarn add vuex-light

via cdn

<script src="https://unpkg.com/vuex-light@latest"></script>

Getting Started

Create the simplest store:

// store.ts
import { createStore } from 'vuex-light'

// Create a new store instance.
export const store = createStore({
  state: {
    count: 0,
  },
  mutations: {
    increment({ state }) {
      state.count++
    },
  },
})

Now, your can access the store by the following ways:

  1. globalProperty

    // main.ts
    // Adds the $store property that can be accessed in any component instance inside the application.
    app.config.globalProperties.$store = store
    
    app.component('child-component', {
      mounted() {
        console.log(this.$store.state.count)
      },
    })

    Example: Hello world

  2. provide/inject

    // main.ts
    import { createApp } from 'vue'
    import { store } from './store'
    
    const app = createApp({
      inject: ['store'],
      template: `
        <div>
          {{ store.state.count }}
        </div>
      `,
    })
    
    // Sets the store that can be injected into all components within the application.
    app.provide('store', store)

    Example: Counter

  3. useStore

    // store.ts
    // create the `useStore` composition function
    export function useStore() {
      return store
    }
    // in a vue component
    import { defineComponent } from 'vue'
    import { useStore } from './store'
    
    export default defineComponent({
      setup() {
        const { state, mutations } = useStore()
    
        return {
          state,
          mutations,
        }
      },
    })

    Example: Todo MVC

Core API

const store = createStore({
  state: {
    count: 0,
  },
  getters: {
    isOdd({ state, getters }) {
      // ...
    },
  },
  mutations: {
    increment({ state, getters, mutations }, ...payloads) {
      // ...
    },
  },
  actions: {
    incrementIfOdd({ state, getters, mutations, actions }, ...payloads) {
      // ...
    },
  },
})

store.state.count
store.getters.isOdd
store.mutations.increment()
store.actions.incrementIfOdd()

Plugins

createLoggerPlugin

https://js-cosmos.github.io/vuex-light/vuex-light.createloggerplugin.html

createPersistPlugin

https://js-cosmos.github.io/vuex-light/vuex-light.createpersistplugin.html

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE file for details

vuex-light's People

Contributors

dependabot-preview[bot] avatar iendeavor 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.