# vuex-class-component

> "A Type Safe Vuex Module or Store Using ES6 Classes and ES7 Decorators written in TypeScript."

Latest version **2.3.6** (published 2021-11-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install vuex-class-component
pnpm add vuex-class-component
yarn add vuex-class-component
bun add vuex-class-component
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.3.6 |
| Published | 2021-11-23 |
| First published | 2018-12-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 180.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 215 |
| Author | michaelolof |
| Maintainers | michaelolof |
| Keywords | Vuex with Type safety, Typesafe Vuex, Vuex Classes, Vuex State Manager, Vuex with Type Support, TypeScript Vuex, Vuex TypeScript |

## Links

- npm: https://www.npmjs.com/package/vuex-class-component
- Repository: https://github.com/michaelolof/vuex-class-component
- Homepage: https://github.com/michaelolof/vuex-class-component#readme
- Issues: https://github.com/michaelolof/vuex-class-component/issues
- npm.io page: https://npm.io/package/vuex-class-component

## Dependencies (2)

- [rimraf](https://npm.io/package/rimraf.md) ^2.6.3
- [object.getownpropertydescriptors](https://npm.io/package/object.getownpropertydescriptors.md) ^2.0.3

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.3.6 (latest) — 2021-11-23
- 2.3.5 — 2020-07-21
- 2.3.4 — 2020-07-08
- 2.3.3 — 2020-06-09
- 2.3.2 — 2020-05-01
- 2.2.1 — 2019-11-16
- 2.1.3 — 2019-11-14
- 2.1.1 — 2019-11-10
- 2.1.0 — 2019-11-10
- 2.0.6 — 2019-11-10
- 2.0.5 — 2019-11-10
- 2.0.4 — 2019-09-08
- 2.0.3 — 2019-09-08
- 2.0.2 — 2019-09-05
- 2.0.1 — 2019-09-02
- … 24 more at https://npm.io/package/vuex-class-component/versions

## README

# Vuex Class Component
A Type Safe Solution for Vuex Modules using ES6 Classes and ES7 Decorators that works out of the box for TypeScript and JavaScript.

## Installation
```
$ npm install --save vuex-class-component
```

## New API
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.
```js
// user.vuex.ts
const user = {
  namespace: true,
  state: {
    firstname: "Michael",
    lastname: "Olofinjana",
    specialty: "JavaScript",
  },
  mutations: {
    clearName(state ) {
      state.firstname = ""; 
      state.lastname = "";
    } 
  },
  actions: {
    doSomethingAsync(context) { ... }
    doAnotherAsyncStuff(context, payload) { ... }
  },
  getters: {
    fullname: (state) => state.firstname + " " + state.lastname,
    bio: (state) => `Name: ${state.fullname} Specialty: ${state.specialty}`,
  }
}
```

```ts
import { createModule, mutation, action, extractVuexModule } from "vuex-class-component";

const VuexModule = createModule({
  namespaced: "user",
  strict: false,
  target: "nuxt",
})

export class UserStore extends VuexModule {

  private firstname = "Michael";
  private lastname = "Olofinjana";
  specialty = "JavaScript";
  
  @mutation clearName() {
    this.firstname = "";
    this.lastname = "";
  }

  @action async doSomethingAsync() { return 20 }

  @action async doAnotherAsyncStuff(payload) { 
    const number = await this.doSomethingAsyc();
    this.changeName({ firstname: "John", lastname: "Doe" });
    return payload + this.fullName;
  }

  // Explicitly define a vuex getter using class getters.
  get fullname() {
    return this.firstname + " " + this.lastname;
  } 

  // Define a mutation for the vuex getter.
  // NOTE this only works for getters.
  set fullname( name :string ) {
    const names = name.split( " " );
    this.firstname = names[ 0 ];
    this.lastname = names[ 1 ];
  }
  
  get bio() {
    return `Name: ${this.fullname} Specialty: ${this.specialty}`;
  }
}

// store.vuex.ts
export const store = new Vuex.Store({
  modules: {
    ...extractVuexModule( UserStore )
  }
})

// Creating proxies.
const vxm = {
  user: createProxy( store, UserStore ),
}
```

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.

## More Powerful Proxies
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: 
```ts
vxm.user.firstname // Michael
vxm.user.firstname = "John";
vxm.user.firstname // John

vxm.user.fullname // John Olofinjana
vxm.user.fullname = "Mad Max";
vxm.user.fullname // Mad Max
vxm.user.firstname // Mad
vxm.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
```html
<!-- App.vue -->
<template>
  <div class>
    <input type="text" v-model="user.firstname" />
    <div>Firstname: {{ user.firstname }}</div>

    <button @click="user.clearName()">Clear Name</button>
    <div>Bio: {{ user.bio }}</div>
  </div>
</template>

<script>
  import { vxm } from "./store";

  export default {
    data() {
      return {
        user: vxm.user,
      }
    }
  }
</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`.

## Implementing More Vuex Functionality
Vuex today has additional functionalities like `$watch` `$subscribe` and `$subScribeAction` respectfully.

This also possible with `vuex-class-component`
```ts
// Watch getters in Vue components
vxm.user.$watch( "fullname", newVal => { 
  console.log( `Fullname has changed: ${newVal}` )
});

// Subscribe to mutations in Vue components 
vxm.user.$subscribe( "clearName", payload => {
  console.log( `clearName was called. payload: ${payload}` )
});

// Subscribe to an action in Vue components
vxm.user.$subscribeAction( "doSomethingAsync", {
  before: (payload :any) => console.log( payload ),
  after: (payload :any) => console.log( payload ),
})
```

We can even do better with Local watchers and subscribers.

```ts
const VuexModule = createModule({
  strict: false,
  target: "nuxt",
  enableLocalWatchers: true,
})

export class UserStore extends VuexModule.With({ namespaced: "user" }) {
  
  firstname = "John";
  lastname = "Doe";
  @mutation changeName( name :string ) { ... }
  @action fetchDetails() { ... }
  get fullname() {
    return this.firstname + " " + this.lastname;
  }

  $watch = {
    fullname( newValue ) { console.log( `Fullname has changed ${newValue}` },
  }

  $subscribe = {
    changeName( payload ) {
      console.log( `changeName was called with payload: ${payload}`)
    }
  }

  $subscribeAction = {
    fetchDetails( payload ) {
      console.log( `fetchDetails action was called with payload: ${payload}` )
    }
  }

}
```



## SubModules Support 
To use submodules
```ts
  const VuexModule = createModule({
    strict: false
  })

  class CarStore extends VuexModule.With({ namespaced: "car" }) {
    @getter noOfWheels = 4;

    @action drive() {
      console.log("driving on", this.noOfWheels, "wheels" )
    }
  }
```
We could use this sub module in a class
```ts
  class VehicleStore extends VuexModule.With({ namespaced: "vehicle" }) {
    car = createSubModule( CarStore );
  }
```
Now you can easily use in your Vue Components like:
```ts
  vxm.vehicle.car.drive() // driving on 4 wheels
```

## JavaScript Support
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:
```js
module.exports = {
  ...
  plugins: [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose" : true }]
  ]
}
```
And then use as follows
```js
import { Module, VuexModule, getter, action } from "vuex-class-component/js";
```

## NuxtJS Support
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"`.
```js
export class UserStore extends createModule({ target: "nuxt" }) {
  ...
}
```

## See Old API
[Old API >](old-api-readme.md)

---
_Source: https://npm.io/package/vuex-class-component · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
