npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vuex-class-component-alt

v2.2.1

Published

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

Downloads

4

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.

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