【发布时间】:2020-09-28 06:58:58
【问题描述】:
在我的 vue 应用程序中,我有一个包含一些选项卡的页面。 我想根据不同的路线更改/显示选项卡。
为此,我使用了这个answer 作为参考。
总的来说,这工作正常!我什至可以通过在移动设备上滑动来更改标签(感谢v-tabs-items 上的@change 侦听器。
但是:单击选项卡标签时,<router-view> 加载的组件被安装了两次。滑动时,只安装一次。
原因与 <router-view> 位于 <v-tab-item>s 的循环内有关。
如果我把它放在这个循环之外,子组件会被正确安装一次。 不幸的是,我不能再使用滑动来更改选项卡,因为内容是解耦的。
所以:是否有机会同时拥有这两种功能(动态路由内容和可滑动性)?
谢谢大家!
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>
路由器:
{
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")
}
]
}
【问题讨论】:
标签: javascript vue.js tabs vuetify.js vue-router