P粉6008451632023-08-26 13:08:54
I did this.
<!-- App.vue HTML 部分 --> <li v-for = "(tab, index) in tabs" :key="index" class="nav-link" @click="active_tab = tab" v-bind:class="{ 'active' : tab === active_tab }"> ... </li>
I added all the tabs I wanted in an array called tabs
.
<script> export default { name: 'Home', data() { return { active_tab: "Tab1", tabs: ["Tab1", "Tab2", "Tab3"] } } </script>
This will create a <li>
element for each tab
in the array tabs
.
All elements will have the nav-link
class, and use v-bind
, if the variable active_tab
is equal to tab
, will add active
to the element's class list. Whenever the user clicks one of the tabs, use @click
to change the active_tab
to the tab
that the user clicked.
style
.nav-link { background-color: var(--primary-color); /* 正常时元素的外观 */ } .nav-link.active { background-color: var(--hover-color); /* 当元素处于活动状态时 */ }