Change image icon when active tab changes
P粉448130258
2023-08-29 18:55:29
<p>Currently I have a v-tab that has four tabs with image icons and text. However, when the tab is active, the active tab icon should change to another image. what should I do? </p>
<pre class="brush:php;toolbar:false;"><v-tabs v-model="tabs" class="tabs-menu">
<v-tab
v-for="item in items"
:key="item.id"
>
<img :src="item.icon" />
{{ item.name }}
</v-tab>
</v-tabs>
data() {
return {
tabs: null,
items: [
{ icon: "/planeInactive.svg", name: "plane" },
{ icon: "/hotelInactive.svg", name: "hotel" },
{ icon: "/planehotelInactive.svg", name: "plane hotel" },
{ icon: "/planeInactive.svg", name: "students" },
],
};
},</pre></p>
v-model
and'tab'
make the tab active by default. (I'm using the tab's name attribute inv-model
, but you can use any unique attribute you want. )v-model
to check which tab is active and update the icon using conditional operators. (I used dummy icons, you can use yours. )In this demo, you should see that when the tab is active, a colored flight icon will be displayed, otherwise a black flight icon will be displayed.