# vuex-map-fields
[![Patreon](https://img.shields.io/badge/patreon-donate-blue.svg)](https://www.patreon.com/maoberlehner)
[![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://paypal.me/maoberlehner)
[![Build Status](https://travis-ci.org/maoberlehner/vuex-map-fields.svg?branch=master)](https://travis-ci.org/maoberlehner/vuex-map-fields)
[![Coverage Status](https://coveralls.io/repos/github/maoberlehner/vuex-map-fields/badge.svg?branch=master)](https://coveralls.io/github/maoberlehner/vuex-map-fields?branch=master)
[![GitHub stars](https://img.shields.io/github/stars/maoberlehner/vuex-map-fields.svg?style=social&label=Star)](https://github.com/maoberlehner/vuex-map-fields)
> Enable two-way data binding for form fields saved in a Vuex store.
[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/O4O7U55Y)
## Install
```bash
npm install --save vuex-map-fields
```
### Basic example
The following example component shows the most basic usage, for mapping fields to the Vuex store using two-way data binding with `v-model`, without directly modifying the store itself, but using getter and setter functions internally (as it is described in the official Vuex documentation: [Two-way Computed Property](https://vuex.vuejs.org/en/forms.html#two-way-computed-property)).
#### Store
```js
import Vue from 'vue';
import Vuex from 'vuex';
// Import the `getField` getter and the `updateField`
// mutation function from the `vuex-map-fields` module.
import { getField, updateField } from 'vuex-map-fields';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
fieldA: '',
fieldB: '',
},
getters: {
// Add the `getField` getter to the
// `getters` of your Vuex store instance.
getField,
},
mutations: {
// Add the `updateField` mutation to the
// `mutations` of your Vuex store instance.
updateField,
},
});
```
#### Component
```html
<template>
<div id="app">
<input v-model="fieldA">
<input v-model="fieldB">
</div>
</template>
<script>
import { mapFields } from 'vuex-map-fields';
export default {
computed: {
// The `mapFields` function takes an array of
// field names and generates corresponding
// computed properties with getter and setter
// functions for accessing the Vuex store.
...mapFields([
'fieldA',
'fieldB',
]),
},
};
</script>
```
[![Edit basic example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/94l98vx0p4)
### Nested properties
Oftentimes you want to have nested properties in the Vuex store. `vuex-map-fields` supports nested data structures by utilizing the object dot string notation.
#### Store
```js
import Vue from 'vue';
import Vuex from 'vuex';
import { getField, updateField } from 'vuex-map-fields';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
user: {
firstName: '',
lastName: '',
},
addresses: [
{
town: '',
},
],
},
getters: {
getField,
},
mutations: {
updateField,
},
});
```
#### Component
```html
<template>
<div id="app">
<input v-model="firstName">
<input v-model="lastName">
<input v-model="town">
</div>
</template>
<script>
import { mapFields } from 'vuex-map-fields';
export default {
computed: {
// When using nested data structures, the string
// after the last dot (e.g. `firstName`) is used
// for defining the name of the computed property.
...mapFields([
'user.firstName',
'user.lastName',
// It's also possible to access
// nested properties in arrays.
'addresses[0].town',
]),
},
};
</script>
```
[![Edit nested properties example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/38y88yvoo1)
### Rename properties
Sometimes you might want to give your computed properties different names than what you're using in the Vuex store. Renaming properties is made possible by passing an object of fields to the `mapFields` function instead of an array.
```html
<template>
<div id="app">
<input v-model="userFirstName">
<input v-model="userLastName">
</div>
</template>
<script>
import { mapFields } from 'vuex-map-fields';
export default {
computed: {
...mapFields({
userFirstName: 'user.firstName',
userLastName: 'user.lastName',
}),
},
};
</script>
```
[![Edit rename properties example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/7yowrm6j4x)
### Custom getters and mutations
By default `vuex-map-fields` is searching for the given properties starting from the root of your state object. Depending on the size of your application, the state object might become quite big and therefore updating the state starting from the root might become a performance issue. To circumvent such problems, it is possible to create a custom `mapFields()` function which is configured to access custom mutation and getter functions which don't start from the root of the state object but are accessing a specific point of the state.
#### Store
```js
import Vue from 'vue';
import Vuex from 'vuex';
import { getField, updateField } from 'vuex-map-fields';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
user: {
firstName: '',
lastName: '',
},
},
getters: {
// By wrapping the `getField()` function we're
// able to provide a specific property of the state.
getUserField(state) {
return getField(state.user);
},
},
mutations: {
// Mutating only a specific property of the state
// can be significantly faster than mutating the
// whole state every time a field is updated.
updateUserField(state, field) {
updateField(state.user, field);
},
},
});
```
#### Component
```html
<template>
<div id="app">
<input v-model="firstName">
<input v-model="lastName">
</div>
</template>
<script>
import { createHelpers } from 'vuex-map-fields';
// The getter and mutation types we're providing
// here, must be the same as the function names we've
// used in the store.
const { mapFields } = createHelpers({
getterType: 'getUserField',
mutationType: 'updateUserField',
});
export default {
computed: {
// Because we're providing the `state.user` property
// to the getter and mutation functions, we must not
// use the `user.` prefix when mapping the fields.
...mapFields([
'firstName',
'lastName',
]),
},
};
</script>
```
[![Edit custom getters and mutations example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/4rroy51z8x)
### Vuex modules
Vuex makes it possible to divide the store into modules.
#### Store
```js
import Vue from 'vue';
import Vuex from 'vuex';
import { createHelpers } from 'vuex-map-fields';
// Because by default, getters and mutations in Vuex
// modules, are globally accessible and not namespaced,
// you most likely want to rename the getter and mutation
// helpers because otherwise you can't reuse them in multiple,
// non namespaced modules.
const { getFooField, updateFooField } = createHelpers({
getterType: 'getFooField',
mutationType: 'updateFooField',
});
Vue.use(Vuex);
export default new Vuex.Store({
// ...
modules: {
fooModule: {
state: {
foo: '',
},
getters: {
getFooField,
},
mutations: {
updateFooField,
},
},
},
});
```
#### Component
```html
<template>
<div id="app">
<input v-model="foo">
</div>
</template>
<script>
import { createHelpers } from 'vuex-map-fields';
// We're using the same getter and mutation types
// as we've used in the store above.
const { mapFields } = createHelpers({
getterType: 'getFooField',
mutationType: 'updateFooField',
});
export default {
computed: {
...mapFields(['foo']),
},
};
</script>
```
[![Edit Vuex modules example](https://cod
徐浪老师
- 粉丝: 8519
- 资源: 1万+
最新资源
- VSG并网仿真模型(无负载) 其中包括有功环、无功环、电压电流双闭环等 仿真结果正确,波形完美,仿真结构和稳态运行波形如下 本仿真适于Matlab2021及以上
- COMSOL模型仿真光纤等波导的三维弯曲,模场分布,波束包络方法 Comsol6.1版本自建仿真模型
- 电机控制器,IGBT结温估算(算法+模型)国际大厂机密算法,多年实际应用,准确度良好 高价值知识 能够同时对IGBT内部6个三极管和6个二极管温度进行估计,并输出其中最热的管子对应温度 可用于温度保
- 并网逆变器阻抗建模,扫频模型扫频验证 新能源 变流器 逆变器 逆变器 复现 伍文华博士lunwen 可设置扫描范围、扫描点数 程序附带注释 包括 逆变器仿真模型,阻抗建模程序,扫频程序 效果很好几
- 储能双向DCDC变流器-模型预测控制 储能buck-boost双向dcdc负载 初级控制为下垂控制 电压环才采用PI控制 电流环采用模型预测 附赠模型 参考文献
- 基于深度学习方法去评估锂电池健康状态(SOH)python实现源码+数据集
- ieee33配电网含分布式电源潮流计算 24小时 牛顿拉夫逊法,算例编程matlab 可调节电压器变比, 加入无功补偿装置 同时还可 移动风机 光伏电源位置
- 永磁同步电机PMSM自抗扰控制ADRC控制 转速外环自抗扰ADRC控制(一阶) 内环PI控制. SVPWM 与双闭环PI对比,转速和电流优势明显超调小 送参考lunwen,简单(详细收费)
- comsol仿真流体对电火花放电或电弧的影响 版本6.0,问前询问清楚,联系不 不 模拟击穿放电后等离子体受电极之间流体的影响
- 模电 直流可调稳压电源设计 Multisim14 仿真报告 利用三极管、二极管基本特性,稳压电源知识设计相应模拟电路 (1)用集成芯片制作一个0~15V的直流电源; (2)功率≥12W; (3)
- MATLAB环境联系传感器下的模态参数识别方法自动选峰法,可用于土木,航空航天,机械等领域
- 双闭环直流调速系统如图所示,包含数学和物理模型 整流装置采用三相桥式电路,基本数据如下: 直流电动机:额定电枢电压=220V,额定电枢电流=55A,额定转速=1000r min,电动机电动势系数Ce=
- Simplorer与Maxwell电机联合仿真,包含搭建好的Simplorer电机场路耦合主电路与控制算法(矢量控制SVPWM),包含电路与算法搭建的详细教,程视,频 仿真文件可复制,可将教程中的电
- Prius2004永磁同步电机设 计 报.告: 磁路法、maxwell有限元法、MotorCAD温仿真、应力分析 内容:: 1.Excell设计程序,可以了解这个电机是怎么设计出来的,已知功率转
- 光伏储能 mppt simulink仿真 两级式结构,前级mppt,后级储能控制 采用双向dcdc 变器控制 当光照较低时放电,较高时充电,维系负载电压恒定 兼容matlab2018以上版本
- 120m BLDC有感仿真模型 双闭环控制,带霍尔传感器,其中霍尔处理,相逻辑用代码实现的,容易理解,为方便转化到代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈