vuejs2 - Vuex:对象数组元素的表单绑定?
问题描述
Vuex 有很好的文档。我们在其中看到了如何进行2 向绑定:
// component.vue
...,
computed: {
message: {
get () {
return this.$store.state.obj.message
},
set (value) {
this.$store.commit('updateMessage', value)
}
}
},
...
这很好用,但是如果你想绑定数组对象的属性怎么办?
例如
// state.js
...
listOfThings: [
{
text: 'lets bind this',
value: 0
},
{
text: 'and this is in the $store',
value: 20
}
]
假设我们使用mapState
,那么我们可能希望有一个组件用于用户可以更新商店的事物列表:
或者,是实现此目的的唯一方法,方法是使对象的表单绑定成为其自己的组件,并通过道具传入索引并在表单组件中使用
text: {
get () {
return this.$store.state.listOfThings[this.i].text
},
set (value) {
this.$store.commit('updateText', {i, text: value})
}
}
解决方案
推荐阅读
- swift - 尝试将字符串与我认为是 unicode 后缀的字符串连接起来
- android - flutter downloader generated plugin registrant error
- swift - 图像无法快速上传 S3 存储桶
- python - 许多小桌子包含在一张大桌子中
- java - 每个类都扩展为对象类,但我的程序给我一个错误,即没有为对象类型定义方法 print()
- arm-template - 我可以使用 ARM JSON 脚本对 Azure Stack 上的订阅应用限制吗?
- javascript - React:如何在另一个函数之前更新状态?
- php - 基于php子数组中相同键的数组总和
- rest - 我应该使用 POST 还是 PUT 进行登录请求,为什么?
- package - Bigcommerce Stencil: SyntaxError: Unexpected token ' in JSON at position 63