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-module-decorators-state

v1.2.2

Published

Additional decorators for vuex-module-decorators npm package

Downloads

7

Readme

vuex-module-decorators-state

HitCount npm

1. @State

You need this package if you use:

  • Vue2
  • Typescript
  • vue-class-component or vue-property-decorator
  • vuex-module-decorators

This decorator adds an easy life-hack to maintain strong typing and easy integration for injecting vuex state to vue class-based component. Check this issue

Example

Want to inject your state like this with typesafe?

    @State
    public readonly user!: user;

Check this vue-webpack-typescript or follow the example bellow.

Install

yarn add vuex-module-decorators-state

How to use?

  1. Extract common vuex-module-decorator interfaces into a separate file, let's say types.ts:
// Typescript type of the state you want to inject
export interface Branch {
  "name": string;
}
// Root vuex store.
export interface IRootState {
  github: IGithubState;
}

// Store module you want to inject. If you have only single store module. You won't need interface above
export interface IGithubState {
  branch: Branch;
}
  1. Create your store:

store.ts:

import Vuex, {Store} from "vuex";
import Vue from "vue";
import {Branch, IGithubState, IRootState} from "@/types";
import {Module, Mutation, VuexModule, getModule} from "vuex-module-decorators";

Vue.use(Vuex);

const store: Store<IRootState> = new Store<IRootState>({});

@Module({
  dynamic: true,
  name: "github",
  store,
})
class GithubModule extends VuexModule implements IGithubState {
  public branch: Branch = {name: "Master branch"};
}

export const githubModule: GithubModule = getModule(GithubModule);
  1. Create decorator with factory method by passing githubModule:
import {stateDecoratorFactory} from 'vuex-module-decorators-state'
export const GithubState = stateDecoratorFactory(githubModule);

You don't need to declare type of the var in order for typescript to give you compilation errors if type missmatches. But if you want to have types, there you go:

export const GithubState: <TCT extends (TCT[TPN] extends GithubModule[TPN] ? unknown : never),
  TPN extends (keyof TCT & keyof GithubModule)>(vueComponent: TCT, fileName: TPN) => void =
    stateDecoratorFactory(githubModule);
  1. Apply decorator anywhere in your components:
<template>
 <div>
  {{branch.name}}
</div>
</template>
<script lang="ts">
  import {Component, Vue} from "vue-property-decorator";
  import {GithubState} from "@/store";
  import {Branch} from "@/types";
  
  @Component
  export default class RepoBranches extends Vue {
  
    @GithubState
    public readonly branch!: Branch;
  }
</script>

If you do

import {GithubState} from "@/store";
import {Branch} from "@/types";

class RepoBranches extends Vue  {
    @GithubState
    // Results typescript compilation error because state doesn't exist
    public readonly notExistedState!: Branch;
  
    @GithubState
    // Results typescript compilation error, because type mismatch
    public readonly branch!: string;
}

2. @HandleLoading

This decorators wraps vue component with with block, that wraps the function and does:

  • Triggers loading state to true on start, and false on finish
  • Sets error message if it occurs

Check this vue-webpack-typescript or follow the example bellow.:

import {HandleLoading} from 'vuex-module-decorators-state'

class MyComp extends Vue {
  public serverError: string = ""; // this would result an error string
  public loading: boolean = false; // this would turn to true on start, and to false on finish
  @HandleLoading({errPropNameOrCB: "serverError", loadingPropName: "loading"})
  private async submitForm(): Promise<void> {
    // do some action
  }
}

Build this package and publish:

yarn build
# npm login
npm publish