导航栏活跃状态

2022-01-19  本文已影响0人  autumn_3d55

1. 图示

image.png

根据路由元信息和导航栏数据进行判断,相等即为活跃高亮,不相等则不高亮。

2.实现方法

//公告中心详情页面
      {
        path: '/noticeDetail',
        name: 'NoticeDetail',
        component: resolve => require(['@/views/index/NoticeDetail'], resolve),
        meta: {
          active: '2'
        }
      },
      //帮助中心页面
      {
        path: '/help',
        name: 'Help',
        component: resolve => require(['@/views/index/Help'], resolve),
        meta: {
          active: '3'
        }
      },
<!--顶部导航封装-->
<template>
  <div class="NavTop">
    <div class="box">
      <ul class="list">
        <li
          class="list_item"
          v-for="(i, index) in navList"
          :key="i.id"
          @click="toDetail(i.path)"
        >
          {{ i.key }}
          <span v-show="active == i.id"></span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  naem: "NavTop",
  props: {
    navList: {
      type: Array,
      default: {},
    },
  },
  data() {
    return {
      active: "1",
    };
  },
  methods: {
    toDetail(path) {
      this.$router.push(path);
    },
  },
  watch: {
    $route() {
      if (this.$route.meta.active) {
        this.active = this.$route.meta.active;
      } else {
        this.active = ''
      }
    },
  },
  created() {
    if (this.$route.meta.active) {
      this.active = this.$route.meta.active;
    }
  },
};
</script>

```navList: [
        {
          id: 1,
          key: '首页',
          path: '/index/home'
        },
        {
          id: 2,
          key: '公告中心',
           path: '/index/noticeCenter'
        },
        {
          id: 3,
          key: '帮助中心',
          path: '/index/help'
        },
        
      ]
    }

- 最后,即可实现导航栏活跃高亮。·
上一篇 下一篇

猜你喜欢

热点阅读