Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
V

vuex-smart-module

> 前端框架
Open source

Type safe Vuex module with powerful module features

374 stars0 likes2 views
WebsiteGitHub

About

Type safe Vuex module with powerful module features

vuex-smart-module

Type safe Vuex module with powerful module features. The basic API idea is brought from Sinai.

Features

  • Completely type safe when used with TypeScript without redundancy.
  • Provide a smart way to use modules.
  • Canonical Vuex-like API as possible.

Installation

bash
$ npm install vuex-smart-module

Usage

All examples are written in TypeScript

You create a module with class syntax:

…

Then, create Vuex store instance by using createStore function from vuex-smart-module:

typescript
// store/index.ts

import Vue from 'vue'
import * as Vuex from 'vuex'
import { createStore, Module } from 'vuex-smart-module'
import { foo } from './modules/foo'

Vue.use(Vuex)

// The 1st argument is root module.
// Vuex store options should be passed to the 2nd argument.
export const store = createStore(
  // Root module
  foo,

  // Vuex store options
  {
    strict: process.env.NODE_ENV !== 'production'
  }
)

The created store is a traditional instance of Vuex store - you can use it in the same manner.

typescript
// main.ts

import Vue from 'vue'
import { store } from './store'
import App from './App.vue'

new Vue({
  el: '#app',
  store,
  render: h => h(App)
})

Nested Modules

You can create a nested module as same as Vuex by passing a module object to another module's modules option.

typescript
import { Getters, Module, createStore } from 'vuex-smart-module'

class NestedState {
  value = 'hello'
}

class NestedGetters extends Getters {
  greeting(name: string): string {
    return this.state.value + ', ' + name
  }
}

const nested = new Module({
  state: NestedState,
  getters: NestedGetters
})

const root = new Module({
  modules: {
    nested
  }
})

const store = createStore(root)

console.log(store.state.nested.value) // -> hello
console.log(store.getters['nested/greeting']('John')) // -> hello, John

Nested modules will be namespaced module by default. If you do not want a module to be a namespaced, pass the namespaced: false option to the module's constructor options.

typescript
import { Getters, Module, createStore } from 'vuex-smart-module'

class NestedState {
  value = 'hello'
}

class NestedGetters extends Getters {
  greeting(name: string): string {
    return this.state.value + ', ' + name
  }
}

const nested = new Module({
  // nested module will not be namespaced
  namespaced: false

  state: NestedState,
  getters: NestedGetters
})

const root = new Module({
  modules: {
    nested
  }
})

const store = createStore(root)

console.log(store.state.nested.value) // -> hello
console.log(store.getters.greeting('John')) // -> hello, John

Module Lifecycle and Dependencies

Getters and actions class can have a special method $init which will be called after the module is initialized in a store. The $init hook receives the store instance as the 1st argument. You can pick some external dependencies from it. The following is an example for Nuxt + Axios Module.

typescript
import { Store } from 'vuex'
import { Actions } from 'vuex-smart-module'

class FooActions extends Actions {
  // Declare dependency type
  store: Store

  // Called after the module is initialized
  $init(store: Store): void {
    // Retain store instance for later
    this.store = store
  }

  async fetch(): Promise {
    console.log(await this.store.$axios.$get('...'))
  }
}

There are no rootState, rootGetters and root options on dispatch, commit because they are too difficult to type and the code has implicit dependencies to other modules. In case of you want to use another module in some module, you can create a module context.

…

Nested Module Context

When there are nested modules in your module, you can access them through a module context.

Let's say you have three modules: counter, todo and root where the root module has former two modules as nested modules:

typescript
import { Module, createStore } from 'vuex-smart-module'

// Counter module
const counter = new Module({
  // ...
})

// Todo module
const todo = new Module({
  // ...
})

// Root module
const root = new Module({
  modules: {
    counter,
    todo
  }
})

export const store = createStore(root)

You can access counter and todo contexts through the root context by using modules property.

typescript
import { root, store } from './store'

// Get root context
const ctx = root.context(store)

// You can access counter and todo context through `modules` as well
const counterCtx = ctx.modules.counter
const todoCtx = ctx.modules.todo

counterCtx.dispatch('increment')
todoCtx.dispatch('fetchTodos')

Register Module Dynamically

You can use registerModule to register a module and unregisterModule to unregister it.

typescript
import { registerModule, unregisterModule } from 'vuex-smart-module'
import { store } from './store'
import { foo } from './store/modules/foo'

// register module
registerModule(
  store, // store instance
  ['foo'], // module path. can be string or array of string
  'foo/', // namespace string which will be when put into the store
  foo, // module instance

  // module options as same as vuex registerModule
  {
    preserveState: true
  }
)

// unregister module
unregisterModule(
  store, // store instance
  foo // module instance which you want to unregister
)

Note that the 3rd argument of registerModule, which is the namespace string, must match with the actual namespace that the store resolves. If you pass the wrong namespace to it, component mappers and context api would not work correctly.

Component Mapper

You can generate mapXXX helpers, which are the same interface as Vuex ones, for each associated module by using the createMapper function. The mapped computed properties and methods are strictly typed. So you will not have some typo or pass wrong payloads to them.

typescript
// @/store/modules/foo
import { Module, createMapper } from 'vuex-smart-module'

// Create module
export const foo = new Module({
  // ...
})

// Create mapper
export const fooMapper = createMapper(foo)
typescript
import Vue from 'vue'

// Import foo mapper
import { fooMapper } from '@/store/modules/foo'

export default Vue.extend({
  computed: fooMapper.mapGetters(['double']),

  methods: fooMapper.mapActions({
    incAsync: 'incrementAsync'
  }),

  created() {
    console.log(this.double)
    this.incAsync(undefined)
  }
})

Composable Function

If you prefer composition api for binding a store module to a component, you can create a composable function by using createComposable.

typescript
// @/store/modules/foo
import { Module, createComposable } from 'vuex-smart-module'

// Create module
export const foo = new Module({
  // ...
})

// Create composable function
export const useFoo = createComposable(foo)
typescript
import { defineComponent } from '@vue/composition-api'

// Import useFoo
import { useFoo } from '@/store/modules/foo'

export default defineComponent({
  setup() {
    // Get Foo module's context
    const foo = useFoo()

    console.log(foo.getters.double)
    foo.dispatch('incrementAsync')
  }
})

Method Style Access for Actions and Mutations

this in an action and a module context have actions and mutations properties. They contains module actions and mutations in method form. You can use them instead of dispatch or commit if you prefer method call style over event emitter style.

The method style has several advantages: you can use Go to definition for your actions and mutations and it prints simple and easier to understand errors if you pass a wrong payload type, for example.

Example usage in an action:

typescript
import { Actions } from 'vuex-smart-module'

class FooActions extends Actions {
  increment(amount: number)
    // Call `increment` mutation
    this.mutations.increment(payload)
  }
}

Example usage via a context:

typescript
import Vue from 'vue'

// Import foo module
import { foo } from '@/store/modules/foo'

export default Vue.extend({
  mounted() {
    const ctx = foo.context(this.$store)

    // Call `increment` action
    ctx.actions.increment(1)
  }
})

Using in Nuxt's Modules Mode

You can use Module#getStoreOptions() method to use vuex-smart-module in Nuxt's module mode.

When you have a counter module like the below:

typescript
// store/counter.ts
import { Getters, Actions, Mutations, Module } from 'vuex-smart-module'

export class CounterState {
  count = 0
}

export class CounterGetters extends Getters {
  get double() {
    return this.state.count * 2
  }
}

export class CounterMutations extends Mutations {
  inc() {
    this.state.count++
  }
}

export class CounterActions extends Actions {
  inc() {
    this.commit('inc')
  }
}

export default new Module({
  state: CounterState,
  getters: CounterGetters,
  mutations: CounterMutations,
  actions: CounterActions
})

Construct a vuex-smart-module root module and export the store options acquired with getStoreOptions in store/index.ts. Note that you have to register all nested modules through the root module:

typescript
// store/index.ts
import { Module } from 'vuex-smart-module'
import counter from './counter'

const root = new Module({
  modules: {
    counter
  }
})

export const {
  state,
  getters,
  mutations,
  actions,
  modules,
  plugins
} = root.getStoreOptions()

If you want to extend a store option, you can manually modify it:

typescript
// store/index.ts
const options = root.getStoreOptions()

export const {
  state,
  getters,
  mutations,
  actions,
  modules
} = options

// Add an extra plugin
export const plugins = options.plugins.concat([otherPlugin])

Hot Module Replacement

To utilize hot module replacement for the store created with vuex-smart-module, we provide hotUpdate function.

The below is an example how to use hotUpdate function:

typescript
import { createStore, hotUpdate } from 'vuex-smart-module'
import root from './root'

export const store = createStore(root)

if (module.hot) {
  // accept actions and mutations as hot modules
  module.hot.accept(['./root'], () => {
    // require the updated modules
    // have to add .default here
    const newRoot = require('./root').default

    // swap in the new root module by using `hotUpdate` provided from vuex-smart-module.
    hotUpdate(store, newRoot)
  })
}

Note that you cannot use hotUpdate under Vuex store instance. Use hotUpdate function imported from vuex-smart-module.

Testing

Unit testing getters, mutations and actions

vuex-smart-module provides the inject helper function which allows you to inject mock dependencies into getters, mutations and actions instances. You can inject any properties for test:

…

Mocking modules to test components

When you want to mock some module assets, you can directly inject a mock constructor into the module options. For example, you will test the following component which is using the counter module:

xml

  Increment

In the spec file, mock the mutations option in the counter module. The below is a Jest example but the essential idea holds true for many test frameworks:

…

Mocking nested modules and dependencies

Using dependencies and nested module contexts in Actions requires to mock them in tests.

So you test the following Actions class that has been constructed as described in the section above:

typescript
import { Store } from 'vuex'
import { Actions } from 'vuex-smart-module'

class FooActions extends Actions {
  // Declare dependency type
  store!: Store
  bar!: Context

  // Called after the module is initialized
  $init(store: Store): void {
    // Retain store instance for later
    this.store = store
    this.bar = bar.context(store)
  }

  async fetch(): Pr

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScripttype-safetytypescriptvuejsvuex

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架