All Projects → paroi-tech → Direct Vuex

paroi-tech / Direct Vuex

Licence: cc0-1.0
Use and implement your Vuex store with TypeScript types. Compatible with the Vue 3 composition API.

Programming Languages

typescript
32286 projects

Labels

Projects that are alternatives of or similar to Direct Vuex

Vue Tetris
Use Vue, Vuex to code Tetris.使用 Vue, Vuex 做俄罗斯方块
Stars: ✭ 2,446 (+968.12%)
Mutual labels:  vuex
Aspnetcore Vue Typescript Template
Template AspNetCore with Vue, Vue router, Vuex, TypeScript, Bulma, Sass and Jest
Stars: ✭ 215 (-6.11%)
Mutual labels:  vuex
Vuex Feature Scoped Structure
📈 Feature scoped Vuex modules to have a better organization of business logic code inside Vuex modules based on Large-scale Vuex application structures @3yourmind
Stars: ✭ 218 (-4.8%)
Mutual labels:  vuex
Vue Blog
🎉 基于vue全家桶 + element-ui 构建的一个后台管理集成解决方案
Stars: ✭ 208 (-9.17%)
Mutual labels:  vuex
Vue Fallowfish
🐠vue全家桶仿闲鱼部分布局以及功能实现
Stars: ✭ 211 (-7.86%)
Mutual labels:  vuex
Array Explorer
⚡️ A resource to help figure out what JavaScript array method would be best to use at any given time
Stars: ✭ 2,512 (+996.94%)
Mutual labels:  vuex
Vue Org Chart
Manage and publish your interactive organization chart (orgchart), 100% free and no install required: just copy a folder to any location
Stars: ✭ 207 (-9.61%)
Mutual labels:  vuex
Shop Vue
It's just a shopping cart experiment using VueJS.
Stars: ✭ 225 (-1.75%)
Mutual labels:  vuex
Yoyocms.abpprojecttemplate
一个基于-vue+vuex+vue-router+EF开发的权限管理系统
Stars: ✭ 213 (-6.99%)
Mutual labels:  vuex
Vue Element Starter
Vue starter with Element-UI [READY, unmaintained now]
Stars: ✭ 216 (-5.68%)
Mutual labels:  vuex
Venture Management
一个包含vuejs和nodejs技术的全栈项目
Stars: ✭ 208 (-9.17%)
Mutual labels:  vuex
Nuxt Juejin Project
仿掘金web网站,使用服务端渲染。主要技术:nuxt + koa + vuex + axios + element-ui 。
Stars: ✭ 209 (-8.73%)
Mutual labels:  vuex
Nx Admin
👍 A magical 🐮 ⚔ vue admin,记得star
Stars: ✭ 2,497 (+990.39%)
Mutual labels:  vuex
Paascloud Login Web
模拟商城,完整的购物流程、后端运营平台,使用 spring cloud + vue 全家桶实现快速搭建企业级微服务项目
Stars: ✭ 207 (-9.61%)
Mutual labels:  vuex
Intro To Vue
Workshop Materials for my Introduction to Vue.js Workshop
Stars: ✭ 2,668 (+1065.07%)
Mutual labels:  vuex
Buefy Shop
A sample shop built with Nuxt, Stripe, Firebase and Serverless Functions
Stars: ✭ 207 (-9.61%)
Mutual labels:  vuex
Plugin Graphql
Vuex ORM persistence plugin to sync the store against a GraphQL API.
Stars: ✭ 215 (-6.11%)
Mutual labels:  vuex
Vuex Easy Firestore
Easy coupling of firestore and a vuex module. 2-way sync with 0 boilerplate!
Stars: ✭ 224 (-2.18%)
Mutual labels:  vuex
Vuemmerce
👉 Responsive ecommerce template 🛒 built with Vue.js and Nuxt.js
Stars: ✭ 223 (-2.62%)
Mutual labels:  vuex
Vuex Router Sync
Effortlessly keep vue-router and vuex store in sync.
Stars: ✭ 2,499 (+991.27%)
Mutual labels:  vuex

direct-vuex

Build Status Dependencies Status npm Type definitions GitHub

Use and implement your Vuex store with TypeScript types. Direct-vuex doesn't require classes, therefore it is compatible with the Vue 3 composition API.

Install

First, add direct-vuex to a Vue 2 application:

npm install direct-vuex

Or, in a Vue 3 application:

npm install [email protected]

Create the store

The store can be implemented almost in the same way as usual.

Create the store:

import Vue from "vue"
import Vuex from "vuex"
import { createDirectStore } from "direct-vuex"

Vue.use(Vuex)

const {
  store,
  rootActionContext,
  moduleActionContext,
  rootGetterContext,
  moduleGetterContext
} = createDirectStore({
  // … store implementation here …
})

// Export the direct-store instead of the classic Vuex store.
export default store

// The following exports will be used to enable types in the
// implementation of actions and getters.
export {
  rootActionContext,
  moduleActionContext,
  rootGetterContext,
  moduleGetterContext
}

// The following lines enable types in the injected store '$store'.
export type AppStore = typeof store
declare module "vuex" {
  interface Store<S> {
    direct: AppStore
  }
}

The classic Vuex store is still accessible through the store.original property. We need it to initialize the Vue application:

import Vue from "vue"
import store from "./store"

new Vue({
  store: store.original, // Inject the classic Vuex store.
  // …
}).$mount("#app")

Use typed wrappers from outside the store

From a component, the direct store is accessible through the direct property of the classic store:

const store = context.root.$store.direct // or: this.$store.direct

Or, you can just import it:

import store from "./store"

Then, the old way to call an action:

store.dispatch("mod1/myAction", myPayload)

… is replaced by the following wrapper:

store.dispatch.mod1.myAction(myPayload)

… which is fully typed.

Typed getters and mutations are accessible the same way:

store.getters.mod1.myGetter
store.commit.mod1.myMutation(myPayload)

Notice: The underlying Vuex store can be used simultaneously if you wish, through the injected $store or store.original.

A limitation on how to declare a State

In store and module options, the state property shouldn't be declared with the ES6 method syntax.

Valid:

  state: { p1: string } as Mod1State
  state: (): Mod1State => { p1: string }
  state: function (): Mod1State { return { p1: string } }

Invalid:

  state(): Mod1State { return { p1: string } }

I'm not sure why but TypeScript doesn't infer the state type correctly when we write that.

Implement a Vuex Store with typed helpers

Direct-vuex provides several useful helpers for implementation of the store. They are all optional. However, if you want to keep your classic implementation of a Vuex Store, then direct-vuex needs to infer the literal type of the namespaced property. You can write namespaced: true as true where there is a namespaced property. But you don't need to worry about that if you use defineModule.

In a Vuex Module

The function defineModule is provided solely for type inference. It is a no-op behavior-wise. It expects a module implementation and returns the argument as-is. This behaviour is similar to (and inspired from) the function defineComponent from the composition API.

The generated functions moduleActionContext and moduleGetterContext are factories for creating functions mod1ActionContext and mod1GetterContext, which converts injected action and getter contexts to their direct-vuex equivalent.

Here is how to use defineModule, moduleActionContext and moduleGetterContext:

import { defineModule } from "direct-vuex"
import { moduleActionContext, moduleGetterContext } from "./store"

export interface Mod1State {
  p1: string
}

const mod1 = defineModule({
  state: (): Mod1State => {
    return {
      p1: ""
    }
  },
  getters: {
    p1OrDefault(...args): string {
      const { state, getters, rootState, rootGetters } = mod1GetterContext(args)
      // Here, 'getters', 'state', 'rootGetters' and 'rootState' are typed.
      // Without 'mod1GetterContext' only 'state' would be typed.
      return state.p1 || "default"
    }
  },
  mutations: {
    SET_P1(state, p1: string) {
      // Here, the type of 'state' is 'Mod1State'.
      state.p1 = p1
    }
  },
  actions: {
    loadP1(context, payload: { id: string }) {
      const { dispatch, commit, getters, state } = mod1ActionContext(context)
      // Here, 'dispatch', 'commit', 'getters' and 'state' are typed.
    }
  },
})

export default mod1
const mod1GetterContext = (args: [any, any, any, any]) => moduleGetterContext(args, mod1)
const mod1ActionContext = (context: any) => moduleActionContext(context, mod1)

2 Warnings:

  • Types in the context of actions implies that TypeScript should never infer the return type of an action from the context of the action. Indeed, this kind of typing would be recursive, since the context includes the return value of the action. When this happens, TypeScript passes the whole context to any. Tl;dr; Declare the return type of actions where it exists!
  • For the same reason, declare the return type of getters each time a getter context generated by moduleGetterContext is used!

Get the typed context of a Vuex Getter, but in the root store

The generated function rootGetterContext converts the injected action context to the direct-vuex one, at the root level (not in a module).

  getters: {
    getterInTheRootStore(...args) {
      const { state, getters } = rootGetterContext(args)
      // Here, 'getters', 'state' are typed.
      // Without 'rootGetterContext' only 'state' would be typed.
    }
  }

Get the typed context of a Vuex Action, but in the root store

The generated function rootActionContext converts the injected action context to the direct-vuex one, at the root level (not in a module).

  actions: {
    async actionInTheRootStore(context, payload) {
      const { commit, state } = rootActionContext(context)
      // … Here, 'commit' and 'state' are typed.
    }
  }

Alternatively: Use localGetterContext and localActionContext

Instead of moduleActionContext and moduleGetterContext, which imply circular dependencies, it is possible to use localGetterContext and localActionContext:

import { defineModule, localActionContext, localGetterContext } from "direct-vuex"

const mod1 = defineModule({
  // …
})

export default mod1
const mod1GetterContext = (args: [any, any, any, any]) => localGetterContext(args, mod1)
const mod1ActionContext = (context: any) => localActionContext(context, mod1)

Now there isn't circular dependency, but getter and action contexts don't provide access to rootState, rootGetters, rootCommit, rootDispatch.

Functions localGetterContext and localActionContext can be used in place of rootGetterContext and rootActionContext too.

Use defineGetters

The function defineGetters is provided solely for type inference. It is a no-op behavior-wise. It is a factory for a function, which expects the object of a getters property and returns the argument as-is.

import { defineGetters } from "direct-vuex"
import { Mod1State } from "./mod1" // Import the local definition of the state (for example from the current module)

export default defineGetters<Mod1State>()({
  getter1(...args) {
    const { state, getters, rootState, rootGetters } = mod1GetterContext(args)
    // Here, 'getters', 'state', 'rootGetters' and 'rootState' are typed.
    // Without 'mod1GetterContext' only 'state' would be typed.
  },
})

Note: There is a limitation. The second parameters getters in a getter implementation, is not typed.

Use defineMutations

The function defineMutations is provided solely for type inference. It is a no-op behavior-wise. It is a factory for a function, which expects the object of a mutations property and returns the argument as-is.

import { defineMutations } from "direct-vuex"
import { Mod1State } from "./mod1" // Import the local definition of the state (for example from the current module)

export default defineMutations<Mod1State>()({
  SET_P1(state, p1: string) {
    // Here, the type of 'state' is 'Mod1State'.
    state.p1 = p1
  }
})

Use defineActions

The function defineActions is provided solely for type inference. It is a no-op behavior-wise. It expects the object of an actions property and returns the argument as-is.

import { defineActions } from "direct-vuex"

export default defineActions({
  loadP1(context, payload: { id: string }) {
    const { dispatch, commit, getters, state } = mod1ActionContext(context)
    // Here, 'dispatch', 'commit', 'getters' and 'state' are typed.
  }
})

About Direct-vuex and Circular Dependencies

When the helper moduleActionContext and moduleGetterContext are used, linters may warn about an issue: "Variable used before it's assigned". I couldn't avoid circular dependencies. Action contexts and getter contexts need to be inferred at the store level, because they contain rootState etc.

Here is an example of a Vuex module implementation:

import { moduleActionContext } from "./store"

const mod1 = {
  getters: {
    p1OrDefault(...args) {
      const { state, getters, rootState, rootGetters } = mod1GetterContext(args)
      // …
    }
  },
  actions: {
    loadP1(context, payload: { id: string }) {
      const { commit, rootState } = mod1ActionContext(context)
      // …
    }
  }
}

export default mod1
const mod1ActionContext = (context: any) => moduleActionContext(context, mod1)
const mod1GetterContext = (args: [any, any, any, any]) => moduleGetterContext(args, mod1)

It works because mod1ActionContext (or mod1GetterContext) is not executed at the same time it is declared. It is executed when an action (or a getter) is executed, ie. after all the store and modules are already initialized.

I suggest to disable the linter rule with a comment at the top of the source file.

With TSLint:

// tslint:disable: no-use-before-declare

With ESLint:

/* eslint-disable no-use-before-define */

Notice: A consequence of these circular dependencies is that the main store file must be imported first from the rest of the application. If a Vuex module is imported first, some part of your implementation could be undefined at runtime.

Contribute

With VS Code, our recommended plugin is:

  • TSLint from Microsoft (ms-vscode.vscode-typescript-tslint-plugin)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].