我在使用 vue 更改 JSON 对象的键名“User1”和“User2”时遇到了一些问题。
myJSON = {"User1": {"damage": "10000"}, "User2": {"damage": "10000"}}
users = ["User1","User2","User3","User4"]
这是循环代码和一些注释。实际上问题是我不能在 v-model 中使用“key”...
<div class="mb-3" v-for="(item, key, index) in myJSON">
<el-select v-model="key" filterable placeholder="Select"> // This is doesn't work - it's my problem =)
<el-option
v-for="cl in users"
:key="cl"
:label="cl"
:value="cl">
</el-option>
</el-select>
<el-input-number v-model="item.damage"></el-input-number> // It's okey, damage is changing.
</div>
我已经尝试在以下位置更改 v-model:
v-model="item.key" // JSON after this
myJSON = {"User1": {"damage": "10000", "key": "User3"}, "User2": {"damage": "10000", "key": "User4"}}
v-model="myJSON[key]" // Have error
TypeError: Cannot read property 'myJSON' of undefined
v-model="myJSON[key]" // Same
TypeError: Cannot read property 'myJSON' of undefined
在选择用户后,<el-select>
我需要这样的东西:
{"User2": {"damage": "10000"}, "User3": {"damage": "10000"}}
或者
{"User3333": {"damage": "10000"}, "User312321": {"damage": "10000"}}
但是我的方法不对,请帮忙。