【问题标题】:How to fire an event in mount in Vuejs如何在 Vuejs 中触发挂载事件
【发布时间】:2022-01-13 07:44:15
【问题描述】:

我有一个侧边栏,您可以在下面看到:

<template>
        <section>
            <div class="sidebar">
                <router-link v-for="(element, index) in sidebar" :key="index" :to="{ name: routes[index] }" :class='{active : (index==currentIndex)  }'>{{ element }}</router-link>
            </div>

            <div class="sidebar-content">
                    <div v-if="currentIndex === 0">
                        Profile
                    </div>
                    <div v-if="currentIndex === 1">
                        Meine Tickets
                    </div>
            </div>
        </section>
</template>
<script>

    export default {
        mounted() {
            EventBus.$on(GENERAL_APP_CONSTANTS.Events.CheckAuthentication, () => {
                this.authenticated = authHelper.validAuthentication();
            });
            console.log()
            this.checkRouter();
        },
        data(){
            return {
                currentIndex:0,
                isActive: false,
                sidebar: ["Profile", "Meine Tickets"],
                routes: ["profile", "my-tickets"],
                authenticated: authHelper.validAuthentication(),
            }
        },
        computed: {
            getUser() {
                return this.$store.state.user;
            },
        },
        methods: {
            changeSidebar(index) {
                this.object = this.sidebar[index].products;
                this.currentIndex=index;
            },
            checkRouter() {
                let router = this.$router.currentRoute.name;
                console.log(router);
                if(router == 'profile') {
                    this.currentIndex = 0;
                } else if(router == 'my-tickets') {
                    this.currentIndex = 1;
                }
            },
        },
    }
</script>

因此,当单击侧边栏中的链接时,路由将更改为 'http://.../my-account/profile' 或 'http://.../my-account/my-票”。但问题是 currentIndex 因此没有改变,内容没有改变,我也无法将活动类添加到链接中。那么您认为我可以如何根据路线更改 currentIndex。如果我触发一个事件,你能帮我解决这个问题吗,因为我不知道如何在 Vue 中执行它。我试图编写一个类似 checkRouter() 的函数,但没有成功。为什么你认为它正在发生?我们将不胜感激所有解决方案。

【问题讨论】:

  • 使 currentIndex 成为一个道具和pass it via Route-Definition。您可以使用NavigationGuards 在更改路线时传递它
  • 我不明白,我认为你的回答对我来说有点复杂。如果可以的话,能否用代码给我答案?

标签: vue.js vuejs2 vue-component vue-router vue-router4


【解决方案1】:

只需在任何组件模板中使用this.$routeDocs 。您无需自定义逻辑即可轻松完成checkRouter() currentIndex。看简单的例子:

<div class="sidebar-content">
     <div v-if="$route.name === 'profile'">
          Profile
      </div>
      <div v-if="$route.name === 'my-tickets'">
          Meine Tickets
       </div>
     </div>

【讨论】:

    【解决方案2】:

    如果我理解正确,您希望currentIndex 成为基于当前活动路由的值吗?您可以将其创建为计算属性:

    currentIndex: function(){
        let route = this.$router.currentRoute.name;
        
        if(router == 'profile') {
            return 0;
        } else if(router == 'my-tickets') {
            return 1;
        }
    }
    

    我认为你可以比现在更多地利用 Vue 的反应性,不需要同一个元素的多个副本,你可以让属性是反应性的。

    <div class="sidebar-content">
        {{ sidebar[currentIndex] }}
    </div>
    

    另外,您可能会考虑将 object 作为计算属性,如下所示:

    computed: {
        getUser() {
            return this.$store.state.user;
        },
        object() {
            return this.sidebar[currentIndex].products;
        }
    },
    

    【讨论】:

    • 感谢您的回答,但我在哪里可以使用您在计算属性中创建的对象?
    • @Melvin,这不是正确的做法,您可以在任何组件模板中简单地使用this.$route
    猜你喜欢
    • 1970-01-01
    • 2018-04-28
    • 2020-11-05
    • 2018-05-07
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2020-08-26
    相关资源
    最近更新 更多