1、HTML
html
<template>
<div class="test">
<!-- 菜单 -->
<ul>
<li
v-for="item in menuList"
:key="item.id"
:class="isActive == item.id ? 'isActive' : ''"
@click="clickMenu(item.id)">
<span>{{ item.label }}</span>
</li>
</ul>
<!-- 内容 -->
<div>
<router-view />
</div>
</div>
</template>
2、Script
javascript
<script>
export default {
name: "",
components: {},
props: {},
data() {
return {
menuList: [
{
id: 1,
label: "首页",
path: "/home",
},
{
id: 2,
label: "分类",
path: "/home",
},
{
id: 3,
label: "商城",
path: "/home",
},
{
id: 4,
label: "推荐",
path: "/home",
},
{
id: 5,
label: "喜欢",
path: "/home",
},
],
isActive: 1, // 选中id,默认选中id为1的菜单
};
},
computed: {},
created() {},
mounted() {},
methods: {
clickMenu(id) {
this.isActive = id;
},
},
};
</script>
3、Css
css
<style lang="less" scoped>
.test {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
ul {
width: 100%;
height: 30px;
display: flex;
justify-content: center;
align-items: center;
li {
width: 6%;
height: 100%;
margin: 0px 10px;
border: 1px solid #24724f;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
font-size: 14px;
// hover样式
&:hover {
background-color: #24724f;
color: #fff;
}
}
// 选中样式
.isActive {
background-color: #24724f;
color: #fff;
}
}
}
</style>