【发布时间】: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