I have the following jsfiddle that has two Vuetify tabs. The documentation doesn't show examples on using vue-router
with them.
I found this Medium.com post on how to use Vuetify with vue-router
, which has the following code:
<div id="app">
<v-tabs grow light>
<v-tabs-bar>
<v-tabs-item href="/" router>
<v-icon>motorcycle</v-icon>
</v-tabs-item>
<v-tabs-item href="/dog" router>
<v-icon>pets</v-icon>
</v-tabs-item>
</v-tabs-bar>
</v-tabs>
<router-view />
</div>
However, the code is now outdated as the Vuetify 1.0.13 Tabs documentation doesn't specify a router
prop in their api, so the embedded example in the post doesn't work.
I also found this StackOverflow answer which had the following code:
<v-tabs-item :to="{path:'/path/to/somewhere'}">
However, using the to
prop doesn't work and it's also not listed in the Vuetify api. In contrast, the v-button
Vuetify component does list a to
prop and utilizes vue-router
, so I would expect a vue-router
supported component to support the to
prop.
Digging around in the old old Vuetify 0.17 docs, the to
prop is specified for v-tabs-item
. It seems that support might have been removed in 1.0.13.
How can I use vue-router
with Vuetify tabs?
The template part:
And the js part:
Routes:
See codesanbox example
Update
Holy wow! I asked the Vuetify community to add documentation to their api, and it looks like they just added the
to
prop as well as othervue-router
props to the Vuetify tabs docs. Seriously, the community there is awesome.Original Answer
The folks in the Vuetify community Discord were able to help me out. My updated jsfiddle now has the working code.
Essentially,
v-tab
is a wrapper forrouter-link
, where I assume it uses slots to pass props torouter-link
, so putting theto
prop onv-tab
works fine.The following code is an example of the working code:
html
js
Result
The answers of @roli-roli and @antoni are currect but lacking of a tiny detail. In fact, using their methods (almost equivalent) there is an issue in mobile views; in fact, in such conditions tabs become swipeable. The problem is that swiping won't update the route as expected, passing from Tab A with component A to Tab B with component B; instead, an animation will be fired and the
activeTab
will change, but the router won't update.TL;DR Swiping the
v-tab-item
does not update the router, and you get the same component under each tab.Solutions could be either disable the swipeability of
v-tab-item
or listening to the change event of thetab
component to update the router accordingly. I would advise this second solution (since swiping results pretty handy in some conditions), but I thik that this would trigger twice the router update when clicking on the tab's label.Here's a working example based on @roli-roli answer
Template
Script
Router
Set up as in previous answers.
I'm just adding some animation-related tips here & clarifying the use of
v-tab-items
vsv-tab-item
.If you have noticed, using the working markup as follows prevents the v-tabs tab switch animation to work:
If you want to keep the tab switch animation, this is a way to do it.
The following code works for me