三级导航栏(Vue3组合式API写法)

<template>
  <div id="nav">
    <ul>
      <li @click="handleDivClick">
        <router-link to="/home">首页</router-link>
      </li>
      <li @click="handleDivClick">
        <router-link to="/product">电影</router-link>
        <ul>
          <li v-for="item in topLevelItems" @click="handleDivClick" :key="item.name">
            <router-link :to="item.href">{{ item.name }}</router-link>
            <ul v-if="item.subItems.length > 0">
              <li v-for="subItem in item.subItems" :key="subItem.name">
                <router-link :to="subItem.href">{{ subItem.name }}</router-link>
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";

interface NavItem {
  href: string;
  name: string;
  subItems?: NavItem[];
}

function handleDivClick(event) {
  const link = event.target.querySelector('a');
  if (link) {
    link.click();
  }
}

const topLevelItems = ref<NavItem[]>([]);

// 假设从数据库获取数据的函数
async function fetchNavData() {
  // 模拟从数据库获取数据
  const data = [
    {
      href: "/user",
      name: "科幻",
      subItems: []
    },
    {
      href: "/product",
      name: "悬疑",
      subItems: []
    },
    {
      href: "/product",
      name: "励志",
      subItems: [
        {
          href: "/product",
          name: "风雨哈佛路"
        },
        {
          href: "/product",
          name: "肖申克的救赎"
        },
        {
          href: "/product",
          name: "阿甘正传"
        },
        {
          href: "/product",
          name: "当幸福来敲门"
        }
      ]
    },
    {
      href: "/product",
      name: "爱情",
      subItems: []
    },
    {
      href: "/product",
      name: "惊悚",
      subItems: []
    }
  ];
  topLevelItems.value = data;
}

fetchNavData();
</script>

<style scoped lang="scss">
* {
  margin: 0;
  padding: 0;
  text-decoration: none;
  list-style: none;
  text-align: center;
  color: #000;

  li {
    width: 100px;
    display: block;
    float: left;
    line-height: 30px;
    background-color: orange;

    li {
      width: 300px;
      display: none;
      position: relative;
      background-color: #6ef7f7;

      ul {
        width: 100px;
        height: 30px;
        position: absolute;
        left: 300px;
        top: 0px;

        li {
          background-color: #c56ef7;
        }
      }
    }
  }

  li:hover {
    background-color: #ffd700;

    ul li {
      display: block;
      ul li {
        display: none;
      }
    }

    ul li:hover {
      background-color: #d4f2e7;
      ul li {
        display: block;
      }
      ul li:hover {
        background-color: #e7beff;
      }
    }
  }
}
</style>

仅供娱乐,理性吃瓜。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值