All Projects → michaelolof → Vuex Class Component

michaelolof / Vuex Class Component

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

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Vuex Class Component

Roastapp
Laravel学院 Roast 应用源码
Stars: ✭ 164 (-13.23%)
Mutual labels:  vuex
Lin Cms Vue
🔆 Vue+ElementPlus构建的CMS开发框架
Stars: ✭ 2,341 (+1138.62%)
Mutual labels:  vuex
Ddbuy
🎉Vue全家桶+Vant 搭建大型单页面电商项目.http://ddbuy.7-orange.cn
Stars: ✭ 2,239 (+1084.66%)
Mutual labels:  vuex
Vue People
VuePeople lists and connects Vue.JS developers around the world.
Stars: ✭ 167 (-11.64%)
Mutual labels:  vuex
Symfony Vuejs
Source code of the tutorial "Building a single-page application with Symfony 4 and Vue.js"
Stars: ✭ 170 (-10.05%)
Mutual labels:  vuex
Vue2 Demo
Vue 基于 Genesis + TS + Vuex 实现的 SSR demo
Stars: ✭ 2,072 (+996.3%)
Mutual labels:  vuex
Vue Online Shop Frontend
《从零到部署:用 Vue 和 Express 实现迷你全栈电商应用》全栈代码
Stars: ✭ 163 (-13.76%)
Mutual labels:  vuex
Vue Cheatsheet
Modified version of the official VueMastery cheatsheet
Stars: ✭ 188 (-0.53%)
Mutual labels:  vuex
Chaos
The Chaos Programming Language
Stars: ✭ 171 (-9.52%)
Mutual labels:  typesafe
Vue Movie
基于vue2.0构建的在线电影网【film】,webpack+vue+vuex+keepAlive+muse-ui+cordova 全家桶,打包成APP
Stars: ✭ 175 (-7.41%)
Mutual labels:  vuex
Vuex Simple
A simpler way to write your Vuex store in Typescript
Stars: ✭ 168 (-11.11%)
Mutual labels:  vuex
Xpcms
基于node的cms系统, 后端采用node+koa+redis,并通过本人封装的redis库实现数据操作,前端采用vue+ts+vuex开发,后台管理系统采用react全家桶开发
Stars: ✭ 170 (-10.05%)
Mutual labels:  vuex
Mmf Blog Vue2 Ssr
mmf-blog-vue2 ssr(The service side rendering)
Stars: ✭ 174 (-7.94%)
Mutual labels:  vuex
Typedapi
Build your web API on the type level.
Stars: ✭ 165 (-12.7%)
Mutual labels:  typesafe
Vue Gates
🔒 A Vue.js & Nuxt.js plugin that allows you to use roles and permissions in your components or DOM elements, also compatible as middleware and methods.
Stars: ✭ 184 (-2.65%)
Mutual labels:  vuex
Renren Aui
项目已迁移至rubik-admin。
Stars: ✭ 163 (-13.76%)
Mutual labels:  vuex
Eth Vue
Featured in Awesome Vue [https://github.com/vuejs/awesome-vue], a curated list maintained by vuejs of awesome things related to the Vue.js framework, and Awesome List [https://awesomelists.net/150-Vue.js/3863-Open+Source/18749-DOkwufulueze-eth-vue], this Truffle Box provides everything you need to quickly build Ethereum dApps that have authentication features with vue, including configuration for easy deployment to the Ropsten Network. It's also Gravatar-enabled. Connecting to a running Ganache blockchain network from Truffle is also possible -- for fast development and testing purposes. Built on Truffle 5 and Vue 3, eth-vue uses vuex for state management, vuex-persist for local storage of app state, and vue-router for routing. Authentication functionalities are handled by Smart Contracts running on the Ethereum blockchain.
Stars: ✭ 171 (-9.52%)
Mutual labels:  vuex
Vuex Orm
The Vuex plugin to enable Object-Relational Mapping access to the Vuex Store.
Stars: ✭ 2,308 (+1121.16%)
Mutual labels:  vuex
Vue Trello
Trello clone with Vue.js for educational purposes
Stars: ✭ 185 (-2.12%)
Mutual labels:  vuex
Vue Cnode
一个vuex vue-router vue-resource的单页面应用demo,api来自cnodejs. vue 1
Stars: ✭ 174 (-7.94%)
Mutual labels:  vuex

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.

// 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}`,
  }
}
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:

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

<!-- 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

// 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.

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

  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

  class VehicleStore extends VuexModule.With({ namespaced: "vehicle" }) {
    car = createSubModule( CarStore );
  }

Now you can easily use in your Vue Components like:

  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:

module.exports = {
  ...
  plugins: [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose" : true }]
  ]
}

And then use as follows

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".

export class UserStore extends createModule({ target: "nuxt" }) {
  ...
}

See Old API

Old API >

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].