You need to enable JavaScript to run this app. Vue如何实现点击笔记卡片时将对应数据传递绑定到弹出层UpdateNotes组件
优惠活动
大模型
产品
解决方案
定价
更多

Vue如何实现点击笔记卡片时将对应数据传递绑定到弹出层UpdateNotes组件

实现方案

核心逻辑是通过Vue父子组件的Props特性,把用户点击的笔记数据传递给弹出层组件,具体修改步骤如下:

第一步:修改DisplayNotes.vue

1. 新增当前选中笔记的存储变量

在data中新增currentNote用来存储用户点击的对应笔记数据,同时修正v-for的key值(原代码使用不存在的note.data作为key会导致渲染异常):

data() {
    return {
        flag: true,
        // 新增:存储当前点击的笔记数据
        currentNote: {
            title: '',
            body: ''
        },
        notes: [{
            id: 1,
            title: 'Fundoo',
            body: 'unlimited notes..'
        }, ],
    }
},

2. 修改toggle方法接收点击的笔记参数

同时修复原代码循环生成相同IDblur导致的DOM操作异常,把ID改为类名:

toggle(note){
    // 把当前点击的笔记赋值给存储变量
    this.currentNote = JSON.parse(JSON.stringify(note))
    // 改ID选择器为类选择器,避免重复ID异常
    var blur=document.getElementsByClassName('blur');
    Array.from(blur).forEach(item => item.classList.toggle('active'));

    var popup=document.getElementById('popup');
    popup.classList.toggle('active');
}

3. 修改template结构

把点击事件绑定当前循环的note参数,给子组件传递props:

<template>
<div class="carddisplay-section" >
    <!-- 把id="blur"改为class="blur",点击事件传当前note,key改为note.id -->
    <div  v-for="note in notes" :key="note.id"  class="container note blur" >
        <div @click="toggle(note)" class="card-content">
            <h5>{{note.title}}</h5>
            <p>{{note.body}}</p>
        </div>
        <div class="import-icons">
            <icons class="imported-icons note-icons" />
            <button v-if="flag" class="card-button" type="button" @click="handlesubmit();Togglebtn();">Close</button>
        </div>
    </div>
    <div id="popup">
        <!-- 给弹出层组件传递当前选中的笔记数据,同时绑定关闭事件 -->
        <UpdateNotes :title="currentNote.title" :body="currentNote.body" @close="toggle"/>
    </div>
</div>
</template>

第二步:修改UpdateNotes.vue

1. 新增Props接收父组件传递的数据,同步本地编辑变量

Vue不允许子组件直接修改Props,所以需要新增本地变量绑定表单,监听Props变化同步数据:

import icons from './icons.vue'
export default{
    components:{icons},
    // 新增Props接收父组件传的笔记内容
    props: {
        title: {
            type: String,
            default: ''
        },
        body: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            // 本地绑定表单的变量
            localTitle: '',
            localBody: ''
        }
    },
    watch: {
        // 监听父组件传值变化,同步到本地编辑变量
        title: {
            immediate: true,
            handler(val) {
                this.localTitle = val
            }
        },
        body: {
            immediate: true,
            handler(val) {
                this.localBody = val
            }
        }
    },
    methods:{
      handlesubmit() {
            // 此处可写更新笔记的接口调用逻辑,完成后通知父组件关闭弹窗
            this.$emit('close')
        },
    }
}

2. 修改template的表单绑定变量

<template>
 <div  class="update" >
        <form class="update-note" @submit.prevent="handlesubmit" autocomplete="off">
            <input name="title"  v-model="localTitle" placeholder="Title" />
            <textarea name="content" v-model="localBody" style="resize: none" placeholder="Take a note..." rows="3"></textarea>
            <div class="btm-icons">
                <icons />
                <button id="btn-section"  type="submit" >Close</button>
            </div>
        </form>
    </div>
</template>

内容的提问来源于stack exchange,提问作者Sravani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 12:36:06