vue 实现tab菜单切换

1、目标:

实现切换tab菜单,激活状态,按钮高亮,显示对应的菜单内容

2、实现

javascript 复制代码
<template>
  <div class="tan_menu">
    <ul class="container">
      <li
        class="item"
        v-for="item in tab_menu"
        :key="item.type"
        :class="{ active: current_menu === item.type }"
        @click="selectMenu(item.type)"
      >
        {{ item.label }}
      </li>
      <!-- <li class="item" :class="{ actice: current_menu === 'login' }">登录</li>
      <li class="item" :class="{ actice: current_menu === 'register' }">
        注册
      </li> -->
    </ul>
    <template v-if="current_menu === 'login'">
      <div class="login">这是--登录--内容</div>
    </template>
    <template v-if="current_menu === 'register'">
      <div class="register">这是--注册--内容</div>
    </template>
  </div>
</template>

<script>
export default {
  name: "TabMenu",
  data() {
    return {
      tab_menu: [
        { type: "login", label: "登录" },
        { type: "register", label: "注册" },
      ],
      current_menu: "login",
    };
  },
  methods: {
    selectMenu(type) {
      this.current_menu = type;
    },
  },
};
</script>

<style lang="less" scoped>
// 清除内外边距
* {
  margin: 0;
  padding: 0;
}
.tan_menu {
  // margin: 20px 20px 0;
  .container {
    list-style: none;
    margin: 100px auto;
    width: 200px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    display: flex;
    justify-content: space-around;
    background-color: skyblue;
    .item {
      width: 100px;
      cursor: pointer;
      &.active {
        background-color: pink;
        color: #fff;
      }
    }
  }
  .login,
  .register {
    width: 300px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    border: 2px solid pink;
    margin: 20px auto;
  }
}
</style>

效果:

相关推荐
一坨阿亮3 分钟前
使用e-tree开发树形穿梭框
javascript·vue.js·elementui
excel9 分钟前
为什么需要构建工具(Webpack / Vite 的本质)
前端
lang2015092810 分钟前
Java SAX 流式解析全解:从原理到 EasyExcel 实战
java·前端·javascript
VidDown13 分钟前
视频协议传输全解析:从 HTTP/HTTPS 到 HLS/DASH 的完整旅程
javascript·网络·http·https·编辑器·音视频·视频编解码
Rain50918 分钟前
2.4. PostgreSQL 数据库连接与实战指南
前端·数据库·人工智能·后端·postgresql·数据分析
console.log('npc')18 分钟前
Codex 桌面端接入 Headroom 压缩代理完整教程
前端·vscode
独泪了无痕43 分钟前
Vue集成uuid生成唯一标识实践指南
前端·vue.js
yuanyxh9 小时前
Mac 软件推荐
前端·javascript·程序员
万少9 小时前
AtomCode开发微信小程序《谁去呀》 全流程
前端·javascript·后端
某人辛木9 小时前
Web自动化测试
前端·python·pycharm·pytest