Vuex Decorators
Be aware that this package only supports Vuex Modules.
Thanks to Decorators in Typescript and Babel, we are able to transform the boring old way of writing Vuex into something way better.
Installation
To install the package use one of the following commands:
npm install @averjs/vuex-decorators
yarn add @averjs/vuex-decorators
Babel
In order for the Decorators and the classes to work, you need have a few babel plugins installed. Also keep in mind that if you want to use the @HasGetterAndMutation-Decorator for example, the babel plugins have to be >= 7.2.
- @babel/plugin-proposal-class-properties
- @babel/plugin-proposal-decorators
Typescript
To make this plugin work with Typescript the only thing you have to do is to enable experimentalDecorators in your tsconfig.json.
How it works
The best way of writing Vuex Modules with this package is to create a file for every module and export a class.
export default class MyNewAwesomeVuexModuleFile {
}
Now in order for Vuex to understand what happens in the class you exported, you need to add a few Decorators. You can import them individually but the most important one is @VuexClass, because it initializes the module. The package is also built with Module Reuse in mind. That means that every module state is returning a function to avoid cross module pollution.
Every class also has to have a moduleName variable. This should be the identifier which you want to use in Vuex to access the module.
import { VuexClass } from '@averjs/vuex-decorators';
@VuexClass
export default class MyNewAwesomeVuexModuleFile {
moduleName = 'myAwesomeVuexModule';
}
Every function is getting called internally by using the call() method. That lets you call the states by using this. To keep calling dispatch and commit familiar with the typical vue notation, you can use this.$store inside your actions or getters. For Typescript there is a class called VuexModule, which you can import and extend from to provide you with typings for this.$store. You can find what can be called by this.$store by referring to the Vuex documentation.
import { VuexClass, Action, HasGetterAndMutation, VuexModule } from '@averjs/vuex-decorators';
@VuexClass
export default class MyNewAwesomeVuexModuleFile extends VuexModule {
moduleName = 'myAwesomeVuexModule';
@HasGetterAndMutation variable = 'awesome';
get getAwesomeVariable() {
return this.variable;
}
set setAwesomeVariable(payload) {
this.variable = payload;
}
@Action awesomeAction(test) {
this.$store.commit('variable', test);
}
}
Decorators
- @VuexClass
- @Getter
- @HasGetter
- @Mutation
- @HasGetterAndMutation
- @Action
- @ExportVuexStore
Example
Vuex file
import { VuexClass, VuexModule, Action, Getter, Mutation, HasGetterAndMutation } from '@averjs/vuex-decorators';
import axios from 'axios';
import map from 'lodash/map';
@VuexClass
export default class TestStore extends VuexModule {
moduleName = 'test';
test = 'test';
@HasGetterAndMutation testArray = [];
get getTest() {
return map(this.test, test => test = '');
}
set setTest(payload) {
this.test = payload
}
@Getter getterTest() {
return map(this.test, test => test = '');
}
@Mutation mutationTest(payload) {
this.test = payload;
}
@Action async fetchTest() {
try {
const { data } = await axios({
url: `test`,
method: 'GET'
});
this.$store.commit('setTest', data.test);
} catch (err) {
throw err;
}
}
}
Usage with Vuex
import Vuex from 'vuex';
import { ExportVuexStore } from '@averjs/vuex-decorators';
import VuexFile from './VuexFile.js';
const vuexFile = ExportVuexStore(VuexFile);
new Vuex.Store({
modules: {
[vuexFile.moduleName]: vuexFile
}
});
ToDo