vuex-bound
Vue two-way binding (v-model) for Vuex state and mutations.
1,628
44
| Install | |
|---|---|
npm install vuex-bound |
|
| Latest Version: | 1.3.2 |
| Vue: | 2 |
| License: | MIT |
| Last Updated: | Dec 7, 2022 |
| Links: | GitHub · npm |
Maintainer: shyam-chen
vuex-bound

Vue two-way binding (v-model) for Vuex state and mutations.
Installation and Usage
$ npm i vuex-bound -S
# or
$ pnpm i vuex-bound -S
# or
$ yarn add vuex-bound
// for commonjs
const { mapModel, updateModel } = require('vuex-bound');
// for es modules
import { mapModel, updateModel } from 'vuex-bound';
Getting Started
Global
/**
* Vuex 3
*/
import Vue from 'vue';
import Vuex from 'vuex';
import createLogger from 'vuex/dist/logger';
import { updateModel } from 'vuex-bound';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
foo: '',
bar: { baz: '' },
db: [
{ mongo: '3' },
],
},
getters: {},
mutations: { ...updateModel() },
actions: {},
plugins: [createLogger({ collapsed: false })],
});
export default store;
/**
* Vuex 4
*/
import { createStore, createLogger } from 'vuex';
import { updateModel } from 'vuex-bound';
export const store = createStore({
state: {
foo: '',
bar: { baz: '' },
db: [
{ mongo: '3' },
],
},
getters: {},
mutations: { ...updateModel() },
actions: {},
plugins: [createLogger({ collapsed: false })],
});
<template>
<div>
<!-- basic -->
<input v-model="foo"> {{ foo }}
<!-- custom name -->
<input v-model="fooCustom"> {{ fooCustom }}
<!-- nested object -->
<input v-model="barBaz"> {{ barBaz }}
<!-- nested object with array -->
<input v-model="mongo"> {{ mongo }}
</div>
</template>
<script>
import { mapModel } from 'vuex-bound';
export default {
computed: {
// your model
...mapModel(['foo']),
// equal to
...mapModel({ foo: state => state.foo }),
...mapModel({
// custom name
fooCustom: state => state.foo,
// nested object
barBaz: state => state.bar.baz,
// nested object with array
mongo: state => state.db[0].mongo,
}),
},
};
</script>
:warning: WARNING
Frankly, state is static, you cannot use destructuring assignment and rename it because it will not be able to update the value.
Modules
/**
* Vuex 3
*/
import Vue from 'vue';
import Vuex from 'vuex';
import createLogger from 'vuex/dist/logger';
import { updateModel } from 'vuex-bound';
Vue.use(Vuex);
const store = new Vuex.Store({
modules: {
a: {
namespaced: true,
modules: {
b: {
namespaced: true,
state: {
foo: '',
bar: { baz: '' },
db: [
{ mongo: '3' },
],
},
actions: {},
mutations: { ...updateModel() },
getters: {},
},
},
},
},
plugins: [createLogger({ collapsed: false })],
});
export default store;
/**
* Vuex 4
*/
import { createStore, createLogger } from 'vuex';
import { updateModel } from 'vuex-bound';
export const store = createStore({
modules: {
a: {
namespaced: true,
modules: {
b: {
namespaced: true,
state: {
foo: '',
bar: { baz: '' },
db: [
{ mongo: '3' },
],
},
actions: {},
mutations: { ...updateModel() },
getters: {},
},
},
},
},
plugins: [createLogger({ collapsed: false })],
});
<template>
<div>
<!-- basic -->
<input v-model="foo"> {{ foo }}
<!-- custom name -->
<input v-model="fooCustom"> {{ fooCustom }}
<!-- nested object -->
<input v-model="barBaz"> {{ barBaz }}
<!-- nested object with array -->
<input v-model="mongo"> {{ mongo }}
</div>
</template>
<script>
import { mapModel } from 'vuex-bound';
export default {
computed: {
// your model
...mapModel('a/b', ['foo']),
// equal to
...mapModel('a/b', { foo: state => state.foo }),
...mapModel('a/b', {
// custom name
fooCustom: state => state.foo,
// nested object
barBaz: state => state.bar.baz,
// nested object with array
mongo: state => state.db[0].mongo,
}),
},
};
</script>
API
mapModel
Type: mapModel(namespace?: string, map: Array<string> | Object<function>): Object
updateModel
Type: updateModel(): Object
Related Packages
direct-vuex
Use and implement your Vuex store with TypeScript types. Compatible with the Vue...
90,286
255
undo-redo-vuex
A Vuex plugin for module namespaced undo and redo functionality. This plugin tak...
9,207
47
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 1.3.2 | ^2.5.16 | MIT | |
| 1.3.1 | ^2.5.16 | MIT | |
| 1.3.0 | ^2.5.16 | MIT | |
| 1.2.2 | ^2.5.16 | MIT | |
| 1.2.1 | ^2.5.16 | MIT | |
| 1.2.0 | ^2.5.16 | MIT | |
| 1.1.2 | ^2.5.16 | MIT | |
| 1.1.1 | ^2.5.16 | MIT | |
| 1.1.0 | ^2.5.16 | MIT | |
| 1.0.0 | ^2.5.16 | MIT | |
| 0.7.0 | ^2.5.16 | MIT | |
| 0.6.1 | ^2.5.16 | MIT | |
| 0.6.0 | ^2.5.16 | MIT | |
| 0.5.0 | ^2.5.16 | MIT | |
| 0.4.3 | ^2.5.16 | MIT | |
| 0.4.2 | ^2.5.16 | MIT | |
| 0.4.1 | ^2.5.16 | MIT | |
| 0.4.0 | ^2.5.16 | MIT | |
| 0.3.0 | ^2.5.16 | MIT | |
| 0.2.1 | MIT | ||
| 0.2.0 | MIT | ||
| 0.1.1 | MIT | ||
| 0.1.0 | MIT |