Gathering detailed insights and metrics for vuex-class-component
Gathering detailed insights and metrics for vuex-class-component
Gathering detailed insights and metrics for vuex-class-component
Gathering detailed insights and metrics for vuex-class-component
A Type Safe Vuex Module or Store Using ES6 Classes and ES7 Decorators written in TypeScript.
npm install vuex-class-component
Typescript
Module System
Node Version
NPM Version
TypeScript (99.77%)
JavaScript (0.23%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
216 Stars
153 Commits
21 Forks
9 Watchers
20 Branches
8 Contributors
Updated on Jul 07, 2025
Latest Version
2.3.6
Package Id
vuex-class-component@2.3.6
Unpacked Size
180.39 kB
Size
25.47 kB
File Count
69
NPM Version
6.14.9
Node Version
14.15.3
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
A Type Safe Solution for Vuex Modules using ES6 Classes and ES7 Decorators that works out of the box for TypeScript and JavaScript.
$ npm install --save vuex-class-component
The goal of the new API is to reduce the decorator overhead and https://github.com/michaelolof/vuex-class-component/issues/27
How we normally define Vuex Stores.
1// user.vuex.ts 2const user = { 3 namespace: true, 4 state: { 5 firstname: "Michael", 6 lastname: "Olofinjana", 7 specialty: "JavaScript", 8 }, 9 mutations: { 10 clearName(state ) { 11 state.firstname = ""; 12 state.lastname = ""; 13 } 14 }, 15 actions: { 16 doSomethingAsync(context) { ... } 17 doAnotherAsyncStuff(context, payload) { ... } 18 }, 19 getters: { 20 fullname: (state) => state.firstname + " " + state.lastname, 21 bio: (state) => `Name: ${state.fullname} Specialty: ${state.specialty}`, 22 } 23}
1import { createModule, mutation, action, extractVuexModule } from "vuex-class-component"; 2 3const VuexModule = createModule({ 4 namespaced: "user", 5 strict: false, 6 target: "nuxt", 7}) 8 9export class UserStore extends VuexModule { 10 11 private firstname = "Michael"; 12 private lastname = "Olofinjana"; 13 specialty = "JavaScript"; 14 15 @mutation clearName() { 16 this.firstname = ""; 17 this.lastname = ""; 18 } 19 20 @action async doSomethingAsync() { return 20 } 21 22 @action async doAnotherAsyncStuff(payload) { 23 const number = await this.doSomethingAsyc(); 24 this.changeName({ firstname: "John", lastname: "Doe" }); 25 return payload + this.fullName; 26 } 27 28 // Explicitly define a vuex getter using class getters. 29 get fullname() { 30 return this.firstname + " " + this.lastname; 31 } 32 33 // Define a mutation for the vuex getter. 34 // NOTE this only works for getters. 35 set fullname( name :string ) { 36 const names = name.split( " " ); 37 this.firstname = names[ 0 ]; 38 this.lastname = names[ 1 ]; 39 } 40 41 get bio() { 42 return `Name: ${this.fullname} Specialty: ${this.specialty}`; 43 } 44} 45 46// store.vuex.ts 47export const store = new Vuex.Store({ 48 modules: { 49 ...extractVuexModule( UserStore ) 50 } 51}) 52 53// Creating proxies. 54const vxm = { 55 user: createProxy( store, UserStore ), 56}
On the surface, it looks like not much has changed. But some rethinking has gone into how the libary works to make for a much better developer experience.
With the strict
option set to false
we can enable greater functionality for our proxies with automatic getters and setters for our state.
For Example:
1vxm.user.firstname // Michael 2vxm.user.firstname = "John"; 3vxm.user.firstname // John 4 5vxm.user.fullname // John Olofinjana 6vxm.user.fullname = "Mad Max"; 7vxm.user.fullname // Mad Max 8vxm.user.firstname // Mad 9vxm.user.lastname // Max
Notice that we didn't have to define a mutation to change the firstname
we just set the state and it updates reactively. This means no more boilerplate mutations for our state, we just mutate them directly.
This also opens up new possibilities in how we consume stores in Vue components. Example
1<!-- App.vue --> 2<template> 3 <div class> 4 <input type="text" v-model="user.firstname" /> 5 <div>Firstname: {{ user.firstname }}</div> 6 7 <button @click="user.clearName()">Clear Name</button> 8 <div>Bio: {{ user.bio }}</div> 9 </div> 10</template> 11 12<script> 13 import { vxm } from "./store"; 14 15 export default { 16 data() { 17 return { 18 user: vxm.user, 19 } 20 } 21 } 22</script>
Notice how much boilerplate has been reduced both in defining our vuex stores and also in using them in our components.
Also notice we no longer need functions like mapState
or mapGetters
.
Vuex today has additional functionalities like $watch
$subscribe
and $subScribeAction
respectfully.
This also possible with vuex-class-component
1// Watch getters in Vue components
2vxm.user.$watch( "fullname", newVal => {
3 console.log( `Fullname has changed: ${newVal}` )
4});
5
6// Subscribe to mutations in Vue components
7vxm.user.$subscribe( "clearName", payload => {
8 console.log( `clearName was called. payload: ${payload}` )
9});
10
11// Subscribe to an action in Vue components
12vxm.user.$subscribeAction( "doSomethingAsync", {
13 before: (payload :any) => console.log( payload ),
14 after: (payload :any) => console.log( payload ),
15})
We can even do better with Local watchers and subscribers.
1const VuexModule = createModule({ 2 strict: false, 3 target: "nuxt", 4 enableLocalWatchers: true, 5}) 6 7export class UserStore extends VuexModule.With({ namespaced: "user" }) { 8 9 firstname = "John"; 10 lastname = "Doe"; 11 @mutation changeName( name :string ) { ... } 12 @action fetchDetails() { ... } 13 get fullname() { 14 return this.firstname + " " + this.lastname; 15 } 16 17 $watch = { 18 fullname( newValue ) { console.log( `Fullname has changed ${newValue}` }, 19 } 20 21 $subscribe = { 22 changeName( payload ) { 23 console.log( `changeName was called with payload: ${payload}`) 24 } 25 } 26 27 $subscribeAction = { 28 fetchDetails( payload ) { 29 console.log( `fetchDetails action was called with payload: ${payload}` ) 30 } 31 } 32 33}
To use submodules
1 const VuexModule = createModule({ 2 strict: false 3 }) 4 5 class CarStore extends VuexModule.With({ namespaced: "car" }) { 6 @getter noOfWheels = 4; 7 8 @action drive() { 9 console.log("driving on", this.noOfWheels, "wheels" ) 10 } 11 }
We could use this sub module in a class
1 class VehicleStore extends VuexModule.With({ namespaced: "vehicle" }) { 2 car = createSubModule( CarStore ); 3 }
Now you can easily use in your Vue Components like:
1 vxm.vehicle.car.drive() // driving on 4 wheels
From version 1.5.0
JavaScript is now supported fully.
To use vuex-class-component in your JavaScript files, ensure your babel.config.js file has the following plugins:
1module.exports = { 2 ... 3 plugins: [ 4 ["@babel/plugin-proposal-decorators", { "legacy": true }], 5 ["@babel/plugin-proposal-class-properties", { "loose" : true }] 6 ] 7}
And then use as follows
1import { Module, VuexModule, getter, action } from "vuex-class-component/js";
From verison 1.6.0
Nuxt is also supported.
To use vuex-class-component
with Nuxt, You add a target
property to the @Module decorator and set it to "nuxt"
.
1export class UserStore extends createModule({ target: "nuxt" }) { 2 ... 3}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 5/21 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
79 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