网站页面刷新后,导航菜单高亮显示的位置不对问题

文章讲述了在Vue项目中实现头部导航时,如何解决页面刷新后高亮状态丢失的问题,通过在VueRouter的beforeEach钩子函数中设置导航目标对应的导航项索引。
摘要由CSDN通过智能技术生成

在写vue项目头部导航时,随便点击头部导航后,页面刷新,高亮状态显示在回到第一个,而不是在保持在刚刚点击的地方

<template>
    <div class="list">
            <ul>
              <li :class="tableIndex == index ? 'activeATable' : ''" 
                v-for="(item, index) in headerList" :key="index"
                @click="clickList(index, item.path)">
                <p>{{ item.name }}</p>
              </li>
            </ul>
    </div>
</template>
<script setup>
import { ref } from 'vue';

//路由引入
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()

//首页导航
const headerList = [
    { path: '/', name: '首页' },
    { path: '/demand', name: '需求大厅' },
    { path: '/service', name: '找服务商' },
    { path: '/resource', name: '资源商城' },
    { path: '/recruit', name: '招募大厅' },
    { path: '/advertisement', name: '广告消息' },
    { path: '/businessCard', name: '群名片' }
]

let tableIndex = ref("0")
const clickList = (index, path) => {
    tableIndex.value = index
    router.push(path)
}


</script>
<style lang="less" scoped>

.list {
    ul {
        max-width: 1296px;
        height: 100px;
        margin-left: 60px;
        list-style: none;
        display: flex;
        align-items: center;
        text-align: center;
    }

    li {
        width: 100px;
        height: 100px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
    }

}

//高亮
.activeATable {
  color: #2EB135;
}
</style>

代码写到这里的时候,表面没有任何的问题 但是在页面刷新后会出现高点丢失的问题 ,下面的解决问题的代码

<script setup>
import { ref } from 'vue';

//路由引入
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()

//首页导航
const headerList = [
    { path: '/', name: '首页' },
    { path: '/a', name: '需求大厅' },
    { path: '/b', name: '找服务商' },
    { path: '/c', name: '资源商城' },
    { path: '/d', name: '招募大厅' },
    { path: '/e', name: '广告消息' },
    { path: '/f', name: '群名片' }
]

//导航栏高亮显示丢失问题解决方案
router.beforeEach((to, from, next) => {
  console.log('当前页面的路由路径:', to.fullPath);
  if (to.fullPath == '/a') {
    tableIndex.value = '1'
  } else if (to.fullPath == '/b') {
    tableIndex.value = '2'
  } else if (to.fullPath == '/c') {
    tableIndex.value = '3'
  } else if (to.fullPath == '/d') {
    tableIndex.value = '4'
  } else if (to.fullPath == '/e') {
    tableIndex.value = '5'
  } else if (to.fullPath == '/f') {
    tableIndex.value = '6'
  } else if (to.fullPath == '/') {
    tableIndex.value = '0'
  } else {
    tableIndex.value = '999'
  }
  next();
});

let tableIndex = ref("0")
const clickList = (index, path) => {
    tableIndex.value = index
    router.push(path)
}


</script>

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果您使用的是 Element UI 的 el-menu 组件,在页面刷新高亮状态不会被保留。这是因为页面刷新后,Vue 组件会被重新渲染,组件的状态会被重置。 为了解决这个问题,您可以在组件的 mounted 钩子函数中获取当前页面的路由信息,然后设置 el-menu 的当前选中项。 具体操作步骤如下: 1. 导入 Vue Router 和 Element UI 组件。 ```javascript import Vue from 'vue' import VueRouter from 'vue-router' import { Menu, Submenu, MenuItem } from 'element-ui' ``` 2. 注册 Vue Router 和 Element UI 组件。 ```javascript Vue.use(VueRouter) Vue.use(Menu) Vue.use(Submenu) Vue.use(MenuItem) ``` 3. 在组件的 mounted 钩子函数中获取当前页面的路由信息,并设置 el-menu 的当前选中项。 ```javascript mounted() { // 获取当前路由信息 const currentPath = this.$route.path // 设置 el-menu 的当前选中项 const menu = this.$refs.menu const activeItem = menu.$el.querySelector(`.el-menu-item[data-path="${currentPath}"]`) if (activeItem) { menu.setCurrentIndex(activeItem.getAttribute('data-index')) } } ``` 4. 在 el-menu 中设置 data-path 属性,用于在 mounted 钩子函数中获取当前页面的路由信息。 ```html <el-menu ref="menu"> <el-menu-item index="1" data-path="/home">首页</el-menu-item> <el-menu-item index="2" data-path="/about">关于我们</el-menu-item> <el-menu-item index="3" data-path="/contact">联系我们</el-menu-item> </el-menu> ``` 这样,当页面刷新时,el-menu 组件会自动设置当前选中项,保持高亮状态不变。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值