鍍金池/ 問答/HTML/ vue切換tab樣式

vue切換tab樣式

像這種結(jié)構(gòu),點(diǎn)擊切換tab樣式該怎么判斷?
圖片描述

html:

<li v-for="(menu,n) in menus" :key="n"">
  <router-link :to="menu.sub?'':menu.href" > {{menu.title}}

數(shù)據(jù)結(jié)構(gòu):

menus: [{
  title: '百度',
  href: 'www.baidu.com',
},{
  title: '開發(fā)手冊(cè)',
  href: 'docs.segmentfault.com',
},{
  title: 'segmentfault',
  href: 'segmentfault.com',
  sub: [{
    title: '問答',
    href: '/questions',
    },{
    title: '專欄',
    href: '/blogs',
    },{
    title: '提問題',
    href: '/ask',
  }]
}]

css樣式:

.avtive {
  background: #f3f3f3;
}
回答
編輯回答
初心

vue-router 用 this.$route, 比如:
<li v-for="(menu,n) in menus" :key="n"" :class="{'active': this.$route.path === menu.href}"></li>


如果普通點(diǎn)擊,可以在加入一個(gè)自段 selectedMenu: “選擇的menu對(duì)象”, 比如:
menus:[{title: '百度', href: 'www.baidu.com'}, {title: '百度2', href: 'www.baidu.com2'}];

<li v-for="(menu,n) in menus" :key="n"" @click="changePath(menu)" :class="{'active': this.selectedMenu.href === menu.href}"></li>

changePath(menu) {

this.selectedMenu = menu;

}

2017年11月9日 21:14