简体   繁体   English

Vue.js如何使用标签加载不同的组件

[英]Vue.js how to use tabs to load different components

I am trying to use a click event on my tabs to set the value of a variable inside data() I then use this variable in an if statement on my components to test the value and display the component if true.我试图在我的选项卡上使用单击事件来设置data()内部变量的值,然后在我的组件的 if 语句中使用此变量来测试该值并在为 true 时显示组件。 But this isn't working and I'm getting no errors.但这不起作用,我没有收到任何错误。 I assume that the value of the variable isn't being set when the click event on the tabs fires.我假设当标签上的点击事件触发时变量的值没有被设置。 Can I not achieve the functionality with this method?我不能用这种方法实现功能吗?

<template>
    <div id="settings_page" class="page_body">
        <h1>Settings</h1>

        <div id="user_info" v-if="user">
            <div id="tabs">
                <div v-on:click="selected_tab == 'account'">Account Details</div>
                <div v-on:click="selected_tab == 'divisions'">Divisions</div>
                <div v-on:click="selected_tab == 'users'">Users</div>
                <div v-on:click="selected_tab == 'columns'">Columns</div>
            </div>

            <div class="content">
                <div v-if="selected_tab == 'account'">
                    <h2>Profile</h2>
                </div>

                <div v-if="selected_tab == 'divisions'">
                    divisions
                </div>

                <div v-if="selected_tab == 'users'">
                    users
                </div>

                <div v-if="selected_tab == 'columns'">
                    columns
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import { mapActions, mapGetters } from 'vuex';

    export default {
        data() {
            return {
                selected_tab: 'account'
            }
        },
        computed:mapGetters(['user']),
        methods: {
            ...mapActions(['getProfile'])
        },
        created() {
            this.getProfile();
        }
    }
</script>

You're doing a comparison in the click event handler:您正在单击事件处理程序中进行比较:

v-on:click="selected_tab == 'account'"

You should use assignment like:您应该使用如下分配:

v-on:click="selected_tab = 'account'"

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

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