Vue.js如何将DisplayNotes组件卡片数据传递到DeleteNote并存入Trash数组
实现方案
你需要通过跨组件状态共享完成该需求,由于项目已经引入了Vuex(main.js中已导入store),直接用Vuex实现最规范,步骤如下:
步骤1:修改Vuex的store配置
打开你的store/index.js文件,添加垃圾桶相关的状态和修改方法:
// store/index.js export default new Vuex.Store({ state: { // 新增:存储垃圾桶里的笔记 trashList: [] }, mutations: { // 新增:把删除的笔记加入垃圾桶 ADD_TRASH_NOTE(state, note) { state.trashList.push({...note}) } } })
如果你不需要全局存储笔记列表,也可以不用把原笔记数据放到store中,直接在DisplayNotes组件本地处理移除逻辑即可。
步骤2:修改icons.vue,抛出删除事件
给垃圾桶图标绑定点击事件,向上触发删除通知:
<template> <div class="footer"> <i class="fas fa-bell"></i> <i class="fas fa-user"></i> <i class="fas fa-palette"></i> <i class="fas fa-image"></i> <i class="fas fa-archive"></i> <!-- 给删除图标加点击事件,向外抛出事件 --> <i class="fas fa-trash" @click="$emit('delete')"></i> </div> </template>
注意:原代码中fa-image的class拼写错误(clss),上面已经修正
步骤3:修改DisplayNotes.vue,处理删除逻辑
监听icons组件抛出的删除事件,拿到当前点击的笔记数据,存入Vuex同时从当前展示列表移除:
<template> <div class="carddisplay-section"> <div v-for="note in notes" :key="note.id" class="container note"> <div @click="toggle(note.id)" class="card-content"> <h5>{{note.title}}</h5> <p>{{note.body}}</p> </div> <div class="import-icons"> <!-- 监听删除事件,传入当前的note对象 --> <icons class="imported-icons note-icons" @delete="handleDelete(note)" /> <button v-if="flag" class="card-button" type="button" @click="handlesubmit();Togglebtn();">Close</button> </div> </div> <div id="popup"> <UpdateNotes :cardId="clickedCard" :cardContent="cardContent" /> </div> </div> </template> <script> import service from '../service/User' import icons from './icons' import UpdateNotes from './UpdateNotes.vue' export default { name: 'DisplayNotes', components: { icons, UpdateNotes }, data() { return { flag: true, notes: [{ id: 1, title: 'Fundoo', body: 'unlimited notes..' }, ], clickedCard: '', cardContent: {}, } }, methods: { Togglebtn() { this.flag = !this.flag; }, async handlesubmit() { service.userDisplayNotes().then(response => { this.notes.push(...response.data); }) }, toggle(id) { var blur = document.getElementById('blur'); blur.classList.toggle('active'); this.clickedCard = id; var popup = document.getElementById('popup'); popup.classList.toggle('active'); }, // 新增删除处理方法 handleDelete(note) { // 把笔记存入Vuex的垃圾桶 this.$store.commit('ADD_TRASH_NOTE', note) // 从当前展示列表移除该笔记 this.notes = this.notes.filter(item => item.id !== note.id) } } } </script>
注意:原代码中v-for循环里给每个卡片都加了相同的id="blur"会导致DOM操作异常,建议把blur改成class属性处理弹窗逻辑
步骤4:修改DeleteNote.vue,从Vuex读取垃圾桶数据
把原来本地的Trash数组替换成从Vuex获取的计算属性:
<template> <div class="Trash-section"> <div v-for="item in Trash" :key="item.id" class="container note"> <div class="delete-content"> <h5>{{item.title}}</h5> <p>{{item.body}}</p> <div class="icons Trash-icons"> <i class="fas fa-trash"></i> <i class="fas fa-trash-restore"></i> </div> </div> </div> </div> </template> <script> export default { name: 'DeleteNote', computed: { // 从Vuex获取垃圾桶数据,替换原来的本地数组 Trash() { return this.$store.state.trashList } } } </script>
轻量替代方案(不用Vuex)
如果你的项目不想用Vuex,也可以用Vue2的事件总线实现:
- 在
main.js里全局挂载事件总线:Vue.prototype.$bus = new Vue() - DisplayNotes组件的handleDelete方法里写:
this.$bus.$emit('note-deleted', note) - DeleteNote组件的mounted钩子监听事件:
this.$bus.$on('note-deleted', note => this.Trash.push({...note}))
内容的提问来源于stack exchange,提问作者Sravani
相关产品推荐
相关产品推荐

