json - 如何提交应该在同一个 Vuex Store 对象中并且未映射到 store 对象的多个嵌套值?
问题描述
如果我想在 Vuex 存储中提交输入值和选定的选项(没有“标签”字符串,以便我发送的对象与我的 Vuex 存储对象匹配),它将如何工作?
模板
<div v-for="(section, indexSections) in sections" :key="indexSections">
<div v-for="(item, indexItem) in section" :key="indexItem">
<div>
<select
v-model="sections[indexSection][indexItem].options"
:options="selectOptions"
></select>
<b-input
type="text"
v-model="sections[indexSection][indexItem].sectionItem"
></b-input>
<b-button @click="removeItem({section,item})"/>
</div>
</div>
<div">
<b-button @click="addNewItem(section)"/>
<b-button @click="addNewSection"/>
</div>
</div>
数据
selectOptions: [
{
options: { option1: true, option2: true },
label: "First"
},
{
options: { option1: false, option2: true },
label: "Second"
}
]
计算
Computed: {
sections: {
get() {
return this.$store.state.sections;
}
}
店铺
sections: [
[{
sectionItem: "",
options: {
strict: true,
includes: true
}
}]
],
解决方案
您不能使用v-model
它来更改 Vuex 状态,这就是突变的用途。
v-model
只是语法糖并处理事件和值的更新。您必须v-on:change
自己实现并调用 Vuex 突变来设置所选选项。
您的selectOptions
阵列看起来不寻常。通常你只有 alabel
和 a value
for 选项。value
是用户选择选项时的选定值。
<template>
<div>
<select @change="onChange">
<option v-for="option in selectOptions" :value="option.value">
{{ option.label }}
</option>
</select>
</div>
</template>
<script>
export default {
data () {
return {
selectOptions: [
//...
],
}
},
methods: {
onChange(event) {
this.$store.commit('setSelectedOption', event.target.value);
},
},
};
</script>
推荐阅读
- azure - Azure AD 联合的 ID 令牌不包含“角色”声明
- echarts - 如何将图像或形状添加到图表?
- arrays - 我想以波形对数组进行排序,但它说 int 不可下标
- java - Ubuntu告诉我找不到spring boot应用程序的init.d服务并且无法运行它
- rpc - 如何在 RSK 上使用 personal_sendTransaction?
- javascript - 如何制作一个在反应中呈现多个图像的图像类
- java - 单选按钮应打开新活动,仅当 edittext 字段不为空时
- node.js - ask-smapi-sdk 身份验证失败 error_description:'客户端身份验证失败',错误:'invalid_client'
- sqlite - sqlite3 在用户函数上返回语法错误
- java - 我怎样才能让它运行?