简体   繁体   English

单击按钮以显示匹配的 DIV 并隐藏其他的,使用 Vue3

[英]Button click to show matching DIV and hide the others, using Vue3

I apologize if question seems dumb.如果问题看起来很愚蠢,我深表歉意。 I'm new to Vue.我是 Vue 的新手。

I have a couple of buttons and a couple of divs.我有几个按钮和几个 div。 Each button when clicked is supposed to display the matching div while hiding everyone else.单击时每个按钮都应该显示匹配的 div,同时隐藏其他所有按钮。

This is what I've done这就是我所做的

<!-- Buttons -->
<div v-for="button in buttons" :key="button" @click="showBox(button.link)">
    {{ button.text }}
</div>

<!-- Boxes -->
<div id="about" :class="{ hidden: boxes.about.isHidden }">
    About me
</div>

<div id="resume" :class="{ hidden: boxes.resume.isHidden }">
    Resume
</div>

<div id="contact" :class="{ hidden: boxes.contact.isHidden }">
    Contact
</div>

<script>
    export default {
        components: {
        },
        props: {
        },
        data () {
            return {
                buttons: [
                    { text: 'About', link: 'about' }, 
                    { text: 'Resume', link: 'resume' },
                    { text: 'Contact', link: 'contact' },
                ],
                boxes: {
                    about: { isHidden: false },
                    resume: { isHidden: true },
                    contact: { isHidden: true },
                }
            }
        },
        methods: {
            showBox(box) {
                boxes.box.isHidden = false
            }
        }
    }
</script>

As you can see, per default the About -box is visible, but I'm not sure how to continue from here.如您所见,默认情况下 About -框是可见的,但我不确定如何从这里继续。 The showBox() method doesn't work when I put the varible (box) I passed from the click-function and place it like that.当我放置从点击函数传递的变量(框)并将其放置时,showBox() 方法不起作用。 I'm also not sure how I best hide the rest of the boxes.我也不确定如何最好地隐藏盒子的 rest。 Do I loop through the objects and set all isHidden to true?我是否循环遍历对象并将所有 isHidden 设置为 true?

Appreciate any help.感谢任何帮助。

You can access data with the this keyword.您可以使用this关键字访问数据。 You need to fixed it with this.boxes[box].isHidden = false你需要用this.boxes[box].isHidden = false修复它

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

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