Gathering detailed insights and metrics for @bartvanvliet/vuex-module-decorators
Gathering detailed insights and metrics for @bartvanvliet/vuex-module-decorators
Gathering detailed insights and metrics for @bartvanvliet/vuex-module-decorators
Gathering detailed insights and metrics for @bartvanvliet/vuex-module-decorators
TypeScript/ES7 Decorators to create Vuex modules declaratively
npm install @bartvanvliet/vuex-module-decorators
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (98.13%)
Shell (1.13%)
JavaScript (0.74%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
271 Commits
2 Forks
1 Watchers
3 Branches
1 Contributors
Updated on Feb 02, 2020
Latest Version
0.9.11
Package Id
@bartvanvliet/vuex-module-decorators@0.9.11
Unpacked Size
680.25 kB
Size
131.15 kB
File Count
21
NPM Version
6.4.1
Node Version
8.15.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
I forked this library from championswimmer/vuex-module-decorators since I required having support for championswimmer/vuex-persist in one of my packages.
Once the pull request I created get's merged I will deprecate this package.
Latest info in: Pull Request
Detailed Guide: https://championswimmer.in/vuex-module-decorators/
Typescript/ES7 Decorators to make Vuex modules a breeze
There are major type-checking changes (could be breaking) in v0.9.7
There are major usage improvements (non backwards compatible) in 0.8.0
Please check CHANGELOG
Read the rest of the README to figure out how to use, or if you readily want to jump into a production codebase and see how this is used, you can check out -
1npm install -D vuex-module-decorators
NOTE This is not necessary for
vue-cli@3
projects, since@vue/babel-preset-app
already includes this plugin
babel-plugin-transform-decorators
experimentalDecorators
to trueimportHelpers: true
in tsconfig.json
emitHelpers: true
in tsconfig.json
target: es5
NOTE Since version
0.9.3
we distribute as ES5, so this section is applicable only to v0.9.2 and below
This package generates code in es2015
format. If your Vue project targets ES6 or ES2015 then
you need not do anything. But in case your project uses es5
target (to support old browsers), then
you need to tell Vue CLI / Babel to transpile this package.
1// in your vue.config.js 2module.exports = { 3 /* ... other settings */ 4 transpileDependencies: ['vuex-module-decorators'] 5}
Remember how vuex modules used to be made ?
1const moduleA = { 2 state: { ... }, 3 mutations: { ... }, 4 actions: { ... }, 5 getters: { ... } 6} 7 8const moduleB = { 9 state: { ... }, 10 mutations: { ... }, 11 actions: { ... } 12} 13 14const store = new Vuex.Store({ 15 modules: { 16 a: moduleA, 17 b: moduleB 18 } 19})
Well not anymore. Now you get better syntax. Inspired by vue-class-component
1import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators' 2 3@Module 4export default class Counter2 extends VuexModule { 5 count = 0 6 7 @Mutation 8 increment(delta: number) { 9 this.count += delta 10 } 11 @Mutation 12 decrement(delta: number) { 13 this.count -= delta 14 } 15 16 // action 'incr' commits mutation 'increment' when done with return value as payload 17 @Action({ commit: 'increment' }) 18 incr() { 19 return 5 20 } 21 // action 'decr' commits mutation 'decrement' when done with return value as payload 22 @Action({ commit: 'decrement' }) 23 decr() { 24 return 5 25 } 26}
Want to see something even better ?
1import { Module, VuexModule, MutationAction } from 'vuex-module-decorators' 2import { ConferencesEntity, EventsEntity } from '@/models/definitions' 3 4@Module 5export default class HGAPIModule extends VuexModule { 6 conferences: Array<ConferencesEntity> = [] 7 events: Array<EventsEntity> = [] 8 9 // 'events' and 'conferences' are replaced by returned object 10 // whose shape must be `{events: [...], conferences: [...] }` 11 @MutationAction({ mutate: ['events', 'conferences'] }) 12 async fetchAll() { 13 const response: Response = await getJSON('https://hasgeek.github.io/events/api/events.json') 14 return response 15 } 16}
1@Module 2class MyModule extends VuexModule { 3 wheels = 2 4 5 @Mutation 6 incrWheels(extra) { 7 this.wheels += extra 8 } 9 10 get axles() { 11 return this.wheels / 2 12 } 13}
this is turned into the equivalent
1const module = { 2 state: { wheels: 2 }, 3 mutations: { 4 incrWheels(state, extra) { 5 state.wheels += extra 6 } 7 }, 8 getters: { 9 axles: (state) => state.wheels / 2 10 } 11}
Use the modules just like you would earlier
1import Vue from 'nativescript-vue' 2import Vuex, { Module } from 'vuex' 3 4import counter from './modules/Counter2' 5import hgapi from './modules/HGAPIModule' 6 7Vue.use(Vuex) 8 9const store = new Vuex.Store({ 10 state: {}, 11 modules: { 12 counter, 13 hgapi 14 } 15})
If you need to support module reuse
or to use modules with NuxtJS, you can have a state factory function generated instead
of a staic state object instance by using stateFactory
option to @Module
, like so:
1@Module({ stateFactory: true }) 2class MyModule extends VuexModule { 3 wheels = 2 4 5 @Mutation 6 incrWheels(extra) { 7 this.wheels += extra 8 } 9 10 get axles() { 11 return this.wheels / 2 12 } 13}
this is turned into the equivalent
1const module = { 2 state() { 3 return { wheels: 2 } 4 }, 5 6 mutations: { 7 incrWheels(state, extra) { 8 state.wheels += extra 9 } 10 }, 11 getters: { 12 axles: (state) => state.wheels / 2 13 } 14}
Vuex allows us to register modules into store at runtime after store is constructed. We can do the following to create dynamic modules
1interface StoreType { 2 mm: MyModule 3} 4// Declare empty store first 5const store = new Vuex.Store<StoreType>({}) 6 7// Create module later in your code (it will register itself automatically) 8// In the decorator we pass the store object into which module is injected 9// NOTE: When you set dynamic true, make sure you give module a name 10@Module({ dynamic: true, store: store, name: 'mm' }) 11class MyModule extends VuexModule { 12 count = 0 13 14 @Mutation 15 incrCount(delta) { 16 this.count += delta 17 } 18}
If you would like to preserve the state e.g when loading in the state from vuex-persist
1... 2 3-- @Module({ dynamic: true, store: store, name: 'mm' }) 4++ @Module({ dynamic: true, store: store, name: 'mm', preserveState: true }) 5class MyModule extends VuexModule { 6 7...
Or when it doesn't have a initial state and you load the state from the localStorage
1... 2 3-- @Module({ dynamic: true, store: store, name: 'mm' }) 4++ @Module({ dynamic: true, store: store, name: 'mm', preserveState: localStorage.getItem('vuex') !== null }) 5class MyModule extends VuexModule { 6 7...
There are many possible ways to construct your modules. Here is one way for drop-in use with NuxtJS (you simply need to add your modules to ~/utils/store-accessor.ts
and then just import the modules from ~/store
):
~/store/index.ts
:
import { Store } from 'vuex'
import { initialiseStores } from '~/utils/store-accessor'
const initializer = (store: Store<any>) => initialiseStores(store)
export const plugins = [initializer]
export * from '~/utils/store-accessor'
~/utils/store-accessor.ts
:
import { Store } from 'vuex'
import { getModule } from 'vuex-module-decorators'
import example from '~/store/example'
let exampleStore: example
function initialiseStores(store: Store<any>): void {
exampleStore = getModule(example, store)
}
export {
initialiseStores,
exampleStore,
}
Now you can access stores in a type-safe way by doing the following from a component or page - no extra initialization required.
import { exampleStore } from '~/store'
...
someMethod() {
return exampleStore.exampleGetter
}
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
128 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More