<template>
<div>
<div class="msg" v-for="msg in messages" :key="msg.id">
<p class="msg-index">[{{ msg.id }}]</p>
<p class="msg-subject" v-text="msg.subject"
v-show="!msg.editing"></p>
<input type="text" name="msg-subject"
v-model="msg.subject" v-show="!!msg.editing">
<p class="msg-body" v-text="msg.body"
v-show="!msg.editing"></p>
<input type="text" name="msg-body" v-model="msg.body"
v-show="!!msg.editing">
<button @click="deleteMsg(msg.id)"> Delete </button>
<button @click="editMsg(msg.id)"> Edit </button>
</div>
</div>
</template>
<script>
... // your usual component code
data(){
return{
messages:{
...
editing:false,
}
}
},
methods: {
EditMsg(id){
this.editing = true;
// you can do a direct axios ajax or fetch to edit the updated value
},
deleteMsg(id){
// you can do a direct axios ajax or fetch to delete value
}
}
... // remaining component code
</script>
旁注:
1=>
不建议使用索引作为键,索引有不同的用途,您可以阅读有关使用索引的内容
Index as a key is an anti-pattern here
.