【问题标题】:How to bind data when user clicks on a particular card it displays on a popup card?当用户单击弹出卡上显示的特定卡时如何绑定数据?
【发布时间】:2021-09-03 06:47:34
【问题描述】:

当用户点击任何包含数据的卡片(数据来自后端 api GET 方法)时,数据应该绑定或显示在弹出卡片上,如何做这件事。在我的情况下,我有两个组件一个是 DisplayNotes.vue,另一个是 UpdateNotes.vue。当用户点击任何显示的卡片时,数据绑定到 updateNotes 卡片(弹出)。如何将该数据传递到弹出卡片,请帮助我解决这个问题。

[DisplayNotes.vue]

<template>
<div class="carddisplay-section" >
    <div  v-for="note in notes" :key="note.data"  id="blur" class="container note">
        <div @click="toggle()" 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/>
    </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..'
            }, ],
        }
    },
    methods: {
        Togglebtn() {
            this.flag = !this.flag;
        },
        async handlesubmit() {
            service.userDisplayNotes().then(response => {
                this.notes.push(...response.data);
            })
        },
        toggle(){
            var blur=document.getElementById('blur');
            blur.classList.toggle('active');

            var popup=document.getElementById('popup');
            popup.classList.toggle('active');
        }
      
    }
}
</script>

<style lang="scss">
.carddisplay-section{
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;
    align-content: space-around;
    gap: 10px;
}
.container {
    height: 180px;
    background: #fff;
    padding: 7px;
    border-radius: 7px;
    box-shadow: 5px 5px 10px #e0dede;
    margin-top: 5%;
    margin-left: 18%;
    margin-right: -15%;
    float: left;
    width: 22%;
}
.card-content {
    h5 {
        font-size: 20px;
        font-weight: 400;
        font-family: 'Times New Roman', Times, serif;
        padding-left: 10px;
    }
    p {
        font-size: 18px;
        width: 90%;
        height: 60px;
        font-family: 'Times New Roman', Times, serif;
        width: 100%;
        border: none;
        padding: 7.5px 10px;
        outline: none;
    }
}
.card-button {
    float: right;
    margin-top: -1px;
    margin-left: 240px;
    font-size: 14px;
    border: none;
    background: none;
    font-family: 'Times New Roman', Times, serif;
}
.note-icons {
    visibility: hidden;
}
.note {
    &:hover {
        .note-icons {
            visibility: visible;
        }
    }
}
.imported-icons {
    margin-top: 10%;
}
#blur.active{
    filter:blur(0.5px);
    // pointer-events: none;
    // user-select: none;
}
#popup{
    position: fixed;
    top:40%;
    left:50%;
    transform: translate(-50%,-50%);
    visibility: hidden;
    opacity: 0;
    transition: 0.5s;
}
#popup.active{
    visibility: visible;
    opacity: 1;
    transition: 0.5s;
}
.card-content p,h5 { 
    word-break: break-word;
    overflow-wrap: break-word;
}
</style>

这是我的 updateNotes.vue(弹出卡片) [UpdateNotes.vue]

<template>
 <div  class="update" >
        <form class="update-note" @submit.prevent="handlesubmit" autocomplete="off">
            <input name="title"  v-model="title" placeholder="Title" />
            <textarea name="content" v-model="body" 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>
<script>
import icons from './icons.vue'
export default{
    components:{icons},
    methods:{
      flip() {
            this.flag = !this.flag;
        },
    }
}

</script>
<style scoped>
.update {
  padding-top: 0%;
  
}

.update-note {
  position: relative;
  width: 550px;
  max-width: 100%;
  margin: 152px auto;
  margin-right: 80%;
  background: rgb(255, 255, 255);
  padding: 15px;
  border-radius: 5px;
  box-shadow: 0 1px 5px #ccc;
}
.update-note input {
  width: 100%;
  max-width: 100%;
  border: none;
  padding: 4px;
  outline: none;
  font-size: 1.2em;
}
textarea {
  width: 100%;
  max-width: 100%;
  border: none;
  padding: 4px;
  outline: none;
  font-size: 1.2em;
}
button {
  border: none;
  background: transparent;
  font-weight: 500;
  float: right;
  margin-top: -5%;
  cursor: pointer;
}

</style>

【问题讨论】:

    标签: javascript html css vue.js sass


    【解决方案1】:

    您只需将要发送的数据作为道具绑定到子组件。在您的情况下,您可以像这样放入组件:

    &lt;UpdateNotes :body="note.body"&gt;&lt;/UpdateNotes&gt;

    然后,除非您想从$refs 获取它,否则您必须在其他组件中定义该道具:

    <script>
    
    export default{
        props: {
            body: String
        }
    }
    
    </script>
    

    请注意,不建议更改道具,因此您可能需要在 data() 部分中复制 body 道具。然后,您可以将该新的(现在是本地的)数据属性绑定到 textarea。在组件的 beforeDestroy 钩子中,您可以调用将本地属性与 prop 进行比较的方法,并使用 this.$emit 通知父组件任何更改。

    【讨论】:

    • 你好@cmc,这里我的父组件是DisplayNotes.vue,子组件是UpdateNote.vue,在哪里添加这个脚本以及何时添加 UpdateNotes> 卡片不可见,如何使用$emit..?
    • 要根据按钮状态使其可见,您可以向子组件添加 v-if 属性。由于我不知道您的 toggle() 函数会做什么,因为它调用了外部函数,因此我无法告诉您如何访问按钮状态。至于$emit:你在child上调用this.$emit('someString', data),用@emit="doSomething"捕获数据。
    • 澄清一下,因为我无法编辑上面的评论:使用this.$emit,您向父级发出一个事件。可以使用 vue 提供的 v-on 句柄捕获此事件。我在上面的评论中歪曲了语法。正确的用法是:在孩子this.$emit('someEvent', data),在父母&lt;ChildComponent :prop="someData" v-on:someEvent="doSomething(data)"&gt;&lt;/ChildComponent&gt;。请注意, v-on 之后的部分是您第一次发出的字符串。您还可以使用短语法@someEvent="..."
    • 谢谢@cmc,toggle() 函数,当用户点击任何卡片时,我在这里使用它来进行背景模糊
    • 我认为问题在于你只用toggle()绑定了一个css类。您必须切换绑定到子组件的布尔值才能使其在切换时显示。
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    相关资源
    最近更新 更多