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>

效果:

相关推荐
用户79172867619735 分钟前
opencode-plugin-peers完全指南:OpenCode如何实现 Claude Code式跨会话消息
javascript
八号当铺1 小时前
使用 Figma Agent Kit:插件 + MCP + 还原 Skill,打通本地设计协作
前端·人工智能·ai编程
一心只读圣贤书1 小时前
AI 辅助前端国际化实践:从文案梳理到多语言资源治理
前端
无责任此方_修行中1 小时前
搓了一个国产大模型与 AI Agent 比价工具
前端·后端·ai编程
szephyr1 小时前
腾讯云 ADP 智能体的 Skills 配置变更后没有走灰度,直接全量替换出了问题怎么定位?
java·前端·腾讯云·腾讯云adp
夏雪coding2 小时前
nginx SPA 回落把 CSS 变成了 HTML:一个返回 200 却让样式失效的坑
前端·nginx
光影少年2 小时前
React Native网络、存储与原生能力
前端·react native·react.js
bonibabi2 小时前
基于 CopilotKit + Java SSE 构建 AI Agent 的前端实践指南
前端·agent
砺能2 小时前
谷歌浏览器中出现部分字体乱码问题
前端
To_OC2 小时前
绕开层层 props 搬运:我把 useContext 和自定义 Hook 跑通了
前端·react.js·前端框架