如何从Getnote.vue的notes数组获取ID并更新对应卡片
实现方案
第一步:修改Getnotes.vue(父组件)
- 先修正v-for的key值,原来的
:key="note.data"是错误写法,应该用笔记唯一ID:key="note.id" - 点击卡片时把当前点击的笔记对象传递给弹窗组件,给Updatenote加ref方便调用子组件属性和方法,修改后的代码如下:
<template> <div class="carddisplay-notes"> <!-- 修正key,点击时传递当前note对象 --> <div class="carddisplay-container" v-for="note in notes" :key="note.id"> <div class="carddisplay" @click="togglePopup(note)"> <h3>{{note.title}}</h3> <p>{{note.description}}</p> </div> <div class="carddisplay-icons"> <Icon /> <button class="card-button" type="button" v-if="flag" @click="handleSubmit();ToggleButton();">Close</button> </div> </div> <div class="cardupdate-popup" id="popup"> <!-- 给子组件加ref,监听更新成功事件 --> <Updatenote ref="updateNoteRef" @update-success="refreshNotes" /> </div> </div> </template> <script> import service from '../../service/User' import Icon from '../../components/pages/Icon.vue' import Updatenote from '../../components/pages/Updatenote.vue' export default { name: 'Getnote', components: { Icon, Updatenote }, data() { return { flag: true, notes:[{ id:1, title: 'notes', description: 'display notes' },], } }, methods: { ToggleButton(){ this.flag = !this.flag; }, async handleSubmit() { service.userGetNote().then(response => { this.notes = response.data; // 原有push会导致列表重复,直接覆盖即可 }) }, // 接收点击的note,给子组件赋值后打开弹窗 togglePopup(note){ this.$refs.updateNoteRef.id = note.id this.$refs.updateNoteRef.title = note.title this.$refs.updateNoteRef.description = note.description var popup=document.getElementById('popup'); popup.classList.toggle('active'); }, // 更新成功后刷新列表 refreshNotes(){ this.handleSubmit() } } } </script> <style lang="scss" scoped> @import "@/SCSS/Getnote.scss"; </style>
第二步:修改Updatenote.vue(子组件)
原来的updateNotes方法调用API的参数传错了,User.js里的userUpdateNote要求传两个参数:id和更新数据,另外更新成功后要通知父组件刷新列表,修改后的代码如下:
<template> <div class="updatecard-notes"> <form class="updatecard-container" @submit.prevent="handleSubmit" id="update-id"> <input name="title" id="name-in" v-model="title" placeholder="Title" autocomplete="off" /> <textarea name="content" id="text-in" v-model="description" placeholder="Take a note..." autocomplete="off"></textarea> <Icon /> <button @click="updateNotes()" type="submit">Close</button> </form> </div> </template> <script> import service from '../../service/User' import Icon from '../../components/pages/Icon.vue' export default { name: 'Updatenote', components: { Icon }, data() { return { id:'', title:'', description:'' } }, methods: { updateNotes: function() { const updateData ={ title:this.title, description:this.description }; // 修正API调用参数,第一个传id,第二个传更新数据 service.userUpdateNote(this.id, updateData).then(response => { console.log("update",response); // 更新成功后触发父组件刷新事件,关闭弹窗 this.$emit('update-success') document.getElementById('popup').classList.remove('active') // 清空表单数据 this.id = '' this.title = '' this.description = '' }) }, } } </script> <style scoped> /* 原有样式保持不变 */ .updatecard-container { /* width: 100px; */ padding: 4px 10px; font-size: 1rem; /* margin-left: 200px; */ margin-bottom: -200px; margin-top: -160px; position: fixed; width: 620px; height: 150px; box-shadow: 5px 5px 10px #e0dede; /* margin-left: 731px; */ font-family: 'Times New Roman', Times, serif; } form textarea { width: 100%; border: none; padding: 0px 10px; outline: none; font-size: 1rem; resize: none; } form input { width: 90%; border: none; padding: 4px 10px; margin-bottom: 10px; margin-left: 0px; margin-top: 2px; font-size: 1.1rem; } form button { position: absolute; display: flex; justify-content: center; align-items: center; right: 25px; bottom: 10px; background: white; color: rgb(0, 0, 0); border: none; width: 50px; height: 36px; cursor: pointer; font-size: 1.1rem; font-family: 'Times New Roman', Times, serif; } </style>
额外优化提示
如果你的更新接口没有返回新token的逻辑,可以删掉原来代码里localStorage.setItem('token',response.data.token);这段无用逻辑。
内容的提问来源于stack exchange,提问作者user16172211
相关产品推荐
相关产品推荐

