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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vuex-service

v0.3.10

Published

use vuex like angular service

Readme

vuex-service

Coverage Status

npm vue2

:exclamation: Use vuex like angular service

const Root = this.$$store()
const Todo = this.$$store('Todo')
const {Member, Todo} = this.$$store('Todo, Member')
// import { Store } from 'vuex-service'
// const Todo = Store('Todo', store) // store is vuex store isntance
Todo.todos // state
Todo.all // getters
Todo.active
Todo.completed
Todo.update() // actions
Todo.m.update() // mutations

:exclamation: EventBus

const Todo = this.$$store('Todo')
Todo.$on('hello.*', fn)
Todo.$on(this, 'hello.*', fn) // auto unregister, when vue component destoryed
Todo.$emit('hello.world', data)
Todo.$once('hello.world', fn)
Todo.$broadcast('hello.world', data)
Todo.$off('hello.world', fn)
Todo.$off('hello.world') // remove all

:exclamation: Multiple arguments

// this.$store.dispatch('Todo/update', arg1)
Todo.update(arg1)
update({ commit }, name) {
  console.log(name)
}
// this.$store.dispatch('Todo/update', [ arg1, arg2, ... ])
Todo.update(arg1, arg2, ...)
update({ commit }, [ name, name2, ... ]) {
  console.log(name, name2, ...)
}

:exclamation: Hook

The default setting is off.

Please read the following library for further instructions. https://www.npmjs.com/package/hooks

Vue.use(vuexService, {hook: true})  // default hook = false

// action this.$store.dispatch('Todo/add', data)
const Todo = this.$$store('Todo')
Todo.hook('add', Todo.add)
Todo.pre('add', next => {
  console.log('hook pre action', Todo.todos)
  next()
})
Todo.post('add', next => {
  console.log('hook post action', Todo.todos)
  next()
})

Todo.add({title: 'hello', completed: false})

// will print
// hook pre action []
// hook post action [{...}]


// mutation this.$store.commit('Todo/add', data)
const Todo = this.$$store('Todo')
Todo.hook('m.add', Todo.m.add)
Todo.pre('m.add', next => {
  console.log('hook pre mutation', Todo.todos)
  next()
})
Todo.post('m.add', next => {
  console.log('hook post mutation', Todo.todos)
  next()
})

Todo.m.add('todos', {title: 'hello', completed: false})

// will print
// hook pre mutation []
// hook post mutation [{...}]

Getting Started

NPM

$ npm install vuex-service

Yarn

$ yarn add vuex-service

When used with a module system, you must explicitly install the vuex-service via Vue.use():

// ~/plugins/vuex-service.js
import Vue from 'vue'
import vuexService, { Store } from 'vuex-service'

Vue.use(vuexService, {hook: true})  // default hook = false

// nuxt
export default ({ app }, inject) => {
  inject('$store', Store)
}

how to use

store actions

// ~/store/Todo.js
import { defaultMutations } from 'vuex-service'

export const state = () => ({
  todos: [],
  hello: {
    world: '123'
  }
})

export const getters = {
  all (state) {
    return state.todos
  },
  active (state) {
    return state.todos.filter(todo => !todo.completed)
  },
  completed (state) {
    return state.todos.filter(todo => todo.completed)
  }
}

export const mutations = {
  ...defaultMutations // set, add, update, remove
}

export const actions = {
  add ({ commit }, todo) {
    const Todo = this.$$store('Todo')
    // commit('Todo/add', [ 'todos', todo ])
    Todo.m.add('todos', todo)
  },
  update ({ commit }, [ todo, patch ]) {
    const Todo = this.$$store('Todo')
    // commit('Todo/update', [ todo, patch ])
    Todo.m.update(todo, patch)
  }
}

vue component

export default {
  computed: {
    todos () {
      const Todo = this.$$store('Todo')
      const filter = this.$route.params.slug || 'all'
      // this.$store.getters['Todo/' + filter]
      return Todo[filter]
    }
  },
  methods: {
    doneCheck (todo, e) {
      const Todo = this.$$store('Todo')
      const data = [ todo, {completed: e.target.checked} ]
      // this.dispatch('Todo/update', data)
      Todo.update(data)
      // or
      // this.dispatch('Todo/update', [ todo, {completed: e.target.checked} ])
      Todo.update(todo, {completed: e.target.checked})
      // or
      Todo.m.update(todo, { completed: e.target.checked })
    },
  }
}

vuex store instance

// ~/store/Locale.js
import { defaultMutations } from 'vuex-service'

export const state = () => ({
  locales: ['en', 'kr'],
  locale: 'en'
})
export const mutations = {
  ...defaultMutations // set, add, update, remove
}

// ~/middleware/i18n.js
import { Store } from 'vuex-service'
export default function ({ isHMR, app, store, route, params, error, redirect }) {
  const Locale = Store('Locale', store)
  console.log(Locale.locales, Locale.locale)
  ...
}

default mutations - set, add, remove, update

// ~/store/Todo.js
import { defaultMutations } from 'vuex-service'

export const state = () => ({
  todos: [],
  hello: {
    world: '123',
    heart: []
  }
})
export const mutations = {
  ...defaultMutations // set, add, update, remove
}
  1. set (prop, value)
const Todo = this.$$store('Todo')
// Todo.m.set(prop, value)
Todo.m.set('todos', [{title: 'todo', completed: false}])
Todo.m.set('hello.world', 'abc') // abc
Todo.m.set('hello.heart', ['two', 'three']) // [two, three]
  1. add (prop, value)
const Todo = this.$$store('Todo')
// Todo.m.add(prop, value)
Todo.m.add('todos', {title: 'todo', completed: false})
Todo.m.add('hello.heart', 'one') // [one]
Todo.m.add('hello.heart', ...['two', 'three']) // [one, two, three]
  1. remove (prop, value)
const Todo = this.$$store('Todo')
// Todo.m.remove(prop, value)
Todo.m.add('todos', {title: 'todo', completed: false})
const todo = Todo.todos[0]
Todo.m.remove('todos', todo) // []
  1. update (src, patch)
const Todo = this.$$store('Todo')
// Todo.m.update(src, patch)
// if src is string, call m.set(prop, value)
Todo.m.update('todos', {title: 'todo', completed: false})
Todo.m.update('hello.heart', 'one') // [one]
Todo.m.update('hello.heart', ['two', 'three'])  // [two, three]
// or
Todo.m.add('todos', {title: 'hi', completed: false})
const todo = Todo.todos[0]
Todo.m.update(todo, {title: 'hello world'}) // 'hi' --> 'hello world'

When you call custom action/mutations

// If two parameters and more
// deprecated
// Todo.update(arg1, arg2) === this.$store.dispatch('Todo/update', {src: arg1, prop: arg1, value: arg2})
// Todo.m.update(arg1, arg2) === this.$store.commit('Todo/update', {src: arg1, prop: arg1, value: arg2})

// changed
// this.$store.dispatch('Todo/update', arg1)
Todo.update(arg1)
update({ commit }, name) {
  console.log(name)
}
// this.$store.dispatch('Todo/update', [ arg1, arg2, ... ])
Todo.update(arg1, arg2, ...)
update({ commit }, [ name, name2, ... ]) {
  console.log(name, name2, ...)
}
// ~/store/Todo.js
import { defaultMutations } from 'vuex-service'

export const state = () => ({
  todos: [],
  hello: {
    world: '123',
    heart: []
  }
})
export const mutations = {
  ...defaultMutations // set, add, update, remove
}
export const actions = {
  update ({ commit }, [ todo, patch ]) {
    const Todo = this.$$store('Todo')
    // commit('Todo/update', [ todo, patch ])
    Todo.m.update(todo, patch)
  }
}
// ~/components/list.vue
export default {
  methods {
    doneCheck (todo, e) {
      const Todo = this.$$store('Todo')
      const data = [ todo, {completed: e.target.checked} ]
      Todo.update(data) // this.dispatch('Todo/update', data)
      // or
      Todo.update(todo, {completed: e.target.checked}) // this.dispatch('Todo/update', [ todo, {completed: e.target.checked} ])
    }
  }
}

module

// ~/store   : nuxt
store
  ├── Todo
  │    └── comments.js
  ├─ Todo.js
  └─ index.js

const Root = this.$$store()
const Todo = this.$$store('Todo')
const comments = this.$$store('Todo/comments')
const { Root, Todo } = this.$$store(' , Todo')
const { Todo, comments } = this.$$store('Todo, Todo/comments')

eventbus

const { Todo, Member } = this.$$store('Todo, Member')

Todo.$on('hello', message => console.log('Todo: ' + message))
Member.$on('hello', message => console.log('Member: ' + message))

Todo.$emit('hello', 'hi')
// Todo: hi

Member.$emit('hello', 'hi')
// Member: hi

Todo.$broadcast('hello', 'hi')
// Todo: hi
// Member: hi

Todo.$on('hello.*', message => console.log('Todo * : ' + message))
Todo.$emit('hello.world', 'hi')
// Todo * : hi

eventbus auto unregister

Alternatively, you can pass a vue scope as the first parameter to on() or once() and the listener will be automatically unregistered if the vue scope is destroyed.

// Passing this will register a this.$on('hook:beforeDestroy) event for you.
// ~/components/todoList.vue
Todo.$on(this, 'hello', fn)
Todo.$once(this, 'hello', fn)

:scroll: Changelog

v0.3.10

  • cache vuexService

v0.3.0

  • add hooks

v0.2.0

  • change arguments of two or more function arguments

v0.1.5

  • support event bus

v0.1.0

  • support vuex like service

:exclamation: Issues

Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.

:muscle: Contribution

Please make sure to read the Contributing Guide before making a pull request.

:copyright: License

MIT