A Store Made with Vue 3 Composition API

A Store Made with Vue 3 Composition API

vuex-light A Store Made with Vue 3 Composition API

vuex-light

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/[email protected]"></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.

Download Details:

Author: js-cosmos

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

Source Code: https://github.com/js-cosmos/vuex-light

vue vuejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

8 Popular Websites That Use The Vue.JS Framework

In this article, we are going to list out the most popular websites using Vue JS as their frontend framework. Vue JS is one of those elite progressive JavaScript frameworks that has huge demand in the web development industry. Many popular websites are developed using Vue in their frontend development because of its imperative features.

Vue Native is a framework to build cross platform native mobile apps using JavaScript

Vue Native is a framework to build cross platform native mobile apps using JavaScript. It is a wrapper around the APIs of React Native. So, with Vue Native, you can do everything that you can do with React Native. With Vue Native, you get

How to Make a Simple Vue Custom Select Component

In this article, you’ll learn how to build a Vue custom select component that can be easily be styled using your own CSS. In fact, it’s the same component that we use in production on Qvault, and you can see it in action on the playground.

Creating a Custom Tooltip Component in Vue

There are plenty of libraries out there that will have you up and running with a good tooltip solution in minutes. However, if you are like me, you are sick and tired of giant dependency trees that have the distinct possibility of breaking at any time.

Vue ShortKey plugin for Vue.js

Vue-ShortKey - The ultimate shortcut plugin to improve the UX .Vue-ShortKey - plugin for VueJS 2.x accepts shortcuts globaly and in a single listener.