In my vue app I have a page containing some tabs. I want to change/show the tabs based on different routes.
For this, I used this answer as a reference.
In general this is working fine! I can even change the tabs by swiping on mobile devices (thanks to the @change
Listener on the v-tabs-items
.
BUT: when clicking on the tab labels, the component loaded by the <router-view>
is getting mounted twice. When swiping, it's only mounted once.
The cause has to do something with the <router-view>
being inside the loop of <v-tab-item>
s.
If I place it outside of this loop, the child components get mounted correctly once. Unfortunatly I then can't change the tabs using swipe anymore, because the content is decoupled.
So: Is there any chance to have both functionalities (dynamic routed content and swipability)?
Thanks guys!
Vue:
<template>
<!-- [...] -->
<v-tabs centered="centered" grow v-model="activeTab">
<v-tab v-for="tab of tabs" :key="tab.id" :id="tab.id" :to="tab.route" exact>
<v-icon>{{ tab.icon }}</v-icon>
</v-tab>
<v-tabs-items v-model="activeTab" @change="updateRouter($event)">
<v-tab-item v-for="tab of tabs" :key="tab.id" :value="resolvePath(tab.route)" class="tab_content">
<!-- prevent loading multiple route-view instances -->
<router-view v-if="tab.route === activeTab" />
</v-tab-item>
</v-tabs-items>
</v-tabs>
<!-- [...] -->
</template>
<script lang="ts">
data: () => ({
activeTab: '',
tabs: [
{id: 'profile', icon: 'mdi-account', route: '/social/profile'},
{id: 'friends', icon: 'mdi-account-group', route: '/social/friends'},
{id: 'settings', icon: 'mdi-cogs', route: '/social/settings'},
]
}),
methods: {
updateRouter(tab:string) {
this.$router.push(tab)
}
},
</script>
Router:
{
path: "/social",
component: () => import("../views/Social.vue"),
meta: {
requiresAuth: true
},
children: [
{
path: "profile",
component: () => import("@/components/social/Profile.vue")
},
{
path: "friends",
component: () => import("@/components/social/Friendlist.vue")
},
{
path: "settings",
component: () => import("@/components/social/ProfileSettings.vue")
}
]
}