vuex-service
❗️ Use vuex like angular service
const Root = thisconst Todo = thisconst Member Todo = this// import { Store } from 'vuex-service'// const Todo = Store('Todo', store) // store is vuex store isntanceTodotodos // stateTodoall // gettersTodoactiveTodocompletedTodo // actionsTodom // mutations
❗️ EventBus
const Todo = thisTodoTodo // auto unregister, when vue component destoryedTodoTodoTodoTodoTodo // remove all
❗️ Multiple arguments
// this.$store.dispatch('Todo/update', arg1)Todo { console}// this.$store.dispatch('Todo/update', [ arg1, arg2, ... ])Todo { console}
❗️ Hook
The default setting is off.
Please read the following library for further instructions.
https://www.npmjs.com/package/hooks
Vue // default hook = false // action this.$store.dispatch('Todo/add', data)const Todo = thisTodoTodoTodo Todo // will print// hook pre action []// hook post action [{...}] // mutation this.$store.commit('Todo/add', data)const Todo = thisTodoTodoTodo Todom // 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 Vue // default hook = false // nuxt { }
how to use
store actions
// ~/store/Todo.js const state = todos: hello: world: '123' const getters = { return statetodos } { return statetodos } { return statetodos } const mutations = ...defaultMutations // set, add, update, remove const actions = { const Todo = this // commit('Todo/add', [ 'todos', todo ]) Todom } { const Todo = this // commit('Todo/update', [ todo, patch ]) Todom }
vue component
computed: { const Todo = this const filter = this$routeparamsslug || 'all' // this.$store.getters['Todo/' + filter] return Todofilter } methods: { const Todo = this const data = todo completed: etargetchecked // this.dispatch('Todo/update', data) Todo // or // this.dispatch('Todo/update', [ todo, {completed: e.target.checked} ]) Todo // or Todom }
vuex store instance
// ~/store/Locale.js const state = locales: 'en' 'kr' locale: 'en'const mutations = ...defaultMutations // set, add, update, remove // ~/middleware/i18n.js { const Locale = console ...}
default mutations - set, add, remove, update
// ~/store/Todo.js const state = todos: hello: world: '123' heart: const mutations = ...defaultMutations // set, add, update, remove
- set (prop, value)
const Todo = this// Todo.m.set(prop, value)TodomTodom // abcTodom // [two, three]
- add (prop, value)
const Todo = this// Todo.m.add(prop, value)TodomTodom // [one]Todom // [one, two, three]
- remove (prop, value)
const Todo = this// Todo.m.remove(prop, value)Todomconst todo = Todotodos0Todom // []
- update (src, patch)
const Todo = this// Todo.m.update(src, patch)// if src is string, call m.set(prop, value)TodomTodom // [one]Todom // [two, three]// orTodomconst todo = Todotodos0Todom // '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 { console}// this.$store.dispatch('Todo/update', [ arg1, arg2, ... ])Todo { console}
// ~/store/Todo.js const state = todos: hello: world: '123' heart: const mutations = ...defaultMutations // set, add, update, removeconst actions = { const Todo = this // commit('Todo/update', [ todo, patch ]) Todom }
// ~/components/list.vue methods { const Todo = this const data = todo completed: etargetchecked Todo // this.dispatch('Todo/update', data) // or Todo // this.dispatch('Todo/update', [ todo, {completed: e.target.checked} ]) }
module
// ~/store : nuxtstore ├── Todo │ └── commentsjs ├─ Todojs └─ indexjs const Root = thisconst Todo = thisconst comments = thisconst Root Todo = thisconst Todo comments = this
eventbus
const Todo Member = this TodoMember Todo// Todo: hi Member// Member: hi Todo// Todo: hi// Member: hi TodoTodo// 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.vueTodoTodo
📜 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
❗️ Issues
Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.
💪 Contribution
Please make sure to read the Contributing Guide before making a pull request.