在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):maoberlehner/vuex-map-fields开源软件地址(OpenSource Url):https://github.com/maoberlehner/vuex-map-fields开源编程语言(OpenSource Language):JavaScript 98.8%开源软件介绍(OpenSource Introduction):vuex-map-fields
Installnpm install --save vuex-map-fields Basic exampleThe following example component shows the most basic usage, for mapping fields to the Vuex store using two-way data binding with Storeimport 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<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> Nested propertiesOftentimes you want to have nested properties in the Vuex store. Storeimport 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<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> Rename propertiesSometimes 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 <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> Custom getters and mutationsBy default Storeimport 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<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> Vuex modulesVuex makes it possible to divide the store into modules. Storeimport 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<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> Namespaced Vuex modulesBy default, mutations and getters inside modules are registered under the global namespace – but you can mark modules as Storeimport Vue from 'vue';
import Vuex from 'vuex';
import { getField, updateField } from 'vuex-map-fields';
Vue.use(Vuex);
export default new Vuex.Store({
// ...
modules: {
fooModule: {
namespaced: true,
state: {
foo: '',
},
getters: {
getField,
},
mutations: {
updateField,
},
},
},
}); Component<template>
<div id="app">
<input v-model="foo">
</div>
</template>
<script>
import { createHelpers } from 'vuex-map-fields';
// `fooModule` is the name of the Vuex module.
const { mapFields } = createHelpers({
getterType: 'fooModule/getField',
mutationType: 'fooModule/updateField',
});
export default {
computed: {
...mapFields(['foo']),
},
};
</script> Or you can pass the module namespace string as the first argument of the <template>
<div id="app">
<input v-model="foo">
</div>
</template>
<script>
import { mapFields } from 'vuex-map-fields';
export default {
computed: {
// `fooModule` is the name of the Vuex module.
...mapFields('fooModule', ['foo']),
},
};
</script> Multi-row fieldsIf you want to build a form which allows the user to enter multiple rows of a specific data type with multiple fields (e.g. multiple addresses) you can use the multi-row field mapping function. Storeimport Vue from 'vue';
import Vuex from 'vuex';
import { getField, updateField } from 'vuex-map-fields';
Vue.use(Vuex);
export default new Vuex.Store({
// ...
state: {
addresses: [
{
zip: '12345',
town: 'Foo Town',
},
{
zip: '54321',
town: 'Bar Town',
},
],
},
getters: {
getField,
},
mutations: {
updateField,
},
}); Component<template>
<div id="app">
<div v-for="address in addresses">
<label>ZIP <input v-model="address.zip"></label>
<label>Town <input v-model="address.town"></label>
</div>
</div>
</template>
<script>
import { mapMultiRowFields } from 'vuex-map-fields';
export default {
computed: {
...mapMultiRowFields(['addresses']),
},
};
</script>
2023-10-27 2022-08-15 2022-08-17 2022-09-23 2022-08-13 |
请发表评论