admin管理员组文章数量:1348065
I have a two column layout. The left column has a stacked menu, and when the user selects an option the content displays in the right. What's the best way to set the active
class on the selected menu link?
I have e up with a solution below, but it seems kind of verbose. Each menu item has a class assignment which is conditional upon data.activeTab
. A click event handler updates the value of data.activeTab
.
<template>
<div>
<div class="row">
<div class="col-md-3">
<ul class="nav nav-pills nav-stacked">
<li role="presentation" :class="{active : activeTab == 'option1'}">
<a :href="'/#/sales/' + uuid + '/option1'" @click="updateMenu">Option 1</a>
</li>
<li role="presentation" :class="{active : activeTab == 'option2'}">
<a :href="'/#/sales/' + uuid + '/option2'" @click="updateMenu">Option 2</a>
</li>
</ul>
</div>
<div class="col-md-9">
<router-view></router-view>
</div>
</div>
</div>
</template>
<script>
export default {
data : function () {
return {
activeTab : 'option1'
}
},
props : [
'uuid'
],
methods : {
updateMenu : function (event) {
var str = event.target.toString();
this.activeTab = str.substr(str.lastIndexOf('/') + 1);
}
}
}
</script>
I have a two column layout. The left column has a stacked menu, and when the user selects an option the content displays in the right. What's the best way to set the active
class on the selected menu link?
I have e up with a solution below, but it seems kind of verbose. Each menu item has a class assignment which is conditional upon data.activeTab
. A click event handler updates the value of data.activeTab
.
<template>
<div>
<div class="row">
<div class="col-md-3">
<ul class="nav nav-pills nav-stacked">
<li role="presentation" :class="{active : activeTab == 'option1'}">
<a :href="'/#/sales/' + uuid + '/option1'" @click="updateMenu">Option 1</a>
</li>
<li role="presentation" :class="{active : activeTab == 'option2'}">
<a :href="'/#/sales/' + uuid + '/option2'" @click="updateMenu">Option 2</a>
</li>
</ul>
</div>
<div class="col-md-9">
<router-view></router-view>
</div>
</div>
</div>
</template>
<script>
export default {
data : function () {
return {
activeTab : 'option1'
}
},
props : [
'uuid'
],
methods : {
updateMenu : function (event) {
var str = event.target.toString();
this.activeTab = str.substr(str.lastIndexOf('/') + 1);
}
}
}
</script>
Share
Improve this question
edited Apr 1, 2017 at 0:28
Belmin Bedak
9,2013 gold badges42 silver badges44 bronze badges
asked Mar 31, 2017 at 22:02
DatsunBingDatsunBing
9,07420 gold badges99 silver badges188 bronze badges
0
1 Answer
Reset to default 8Use router-link instead of your manual anchor tags and set the linkActiveClass
in your router construction options.
I can't see your routes object, so this is a best guess based on what you have above. Refer to the router link documentation I linked above to determine the best way to set up your to
attributes.
<router-link tag="li" :to="{ path: 'sales', params: { uuid : uuid, option: "option1" }}">
<a>Option 1</a>
</router-link>
<router-link tag="li" :to="{ path: 'sales', params: { uuid : uuid, option: "option2" }}>
<a>Option 2</a>
</router-link>
Then, wherever you initialized your router,
new VueRouter({
routes,
linkActiveClass: "active"
});
本文标签: javascriptHow to set active menu option with VueStack Overflow
版权声明:本文标题:javascript - How to set active menu option with Vue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743841534a2548366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论