简体   繁体   English

在一个列表项中显示/隐藏元素,而不是全部

[英]Show/hide an element within one list item, not all of them

I've generated a dynamic list of comments on a post from my server in Vue, and would now like a 'reply' button to appear on each comment that when clicked opens a textarea underneath it and is linked to that comment. 我已经在Vue的服务器上生成了一个动态的评论动态列表,现在可以在每个评论上显示一个“回复”按钮,当点击它时会在其下方打开一个textarea并链接到该评论。 However, my hacky solution meant that clicking on it opened all of the comments' text boxes, not just one. 但是,我的hacky解决方案意味着点击它打开了所有评论的文本框,而不仅仅是一个。

How can I target each comment individually with a show/hide function in Vue? 如何使用Vue中的show / hide功能单独定位每个注释?

I know why my solution isn't working—but I don't know where to start making a function that targets the specific comment on clicking it. 我知道为什么我的解决方案不起作用 - 但我不知道从哪里开始制作一个针对点击它的具体评论的功能。

Template (HTML) 模板(HTML)

<ul>
    <li v-for="comment in comments" :key="comment.data.id">
        <div>User details</div>
        <div>Comment content</div>
        <div>
            <span>
                <a v-on:click="hideReply = !hideReply">Reply</a>
            </span>
        </div>

        <form v-if="hideReply">
            <textarea>Reply text box</textarea>
            <button>Reply button</button>
        </form>
    </li>
    <li>Another comment in the list...</li>
    <li>Another comment in the list...</li>
    ...
</ul>

Script (JS) 脚本(JS)

export default {
    name: 'Post',
    components: {},
    data () {
        return{
            hideReply: false,
            comment: undefined,
            comments: undefined
        }
    },
    async created () {
        // code to bring in my comments from server
    },
    methods: {
        betterShowHideFunction () {
            // where do i start
        }
    }
}

Add a show property on each comment and use that for showing/hiding each individual comment 在每个评论上添加show属性,并使用它来显示/隐藏每个评论

<form v-if="comment.show">

Change the var accordingly 相应地更改var

<a v-on:click="comment.show = !comment.show">Reply</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM