解决前端开发路由跳转之页面跳转,左侧菜单栏高亮显示跟随

默认情况:

<template>

  <el-menu :default-active="currentIndex" @select="handleSelect">

    <div v-for="(item, index) in routeList" :key="index">

      <template v-if="item.hidden"></template>

      <template

        v-else-if="

          !item.children ||

          !item.children.length ||

          (item.meta && item.meta.hideChildren)

        "

      >

        <el-menu-item :index="item.path">

          <i :class="item.icon"></i>

          <span slot="title">{{ item.name }}</span>

        </el-menu-item>

      </template>

      <el-submenu :index="item.path" v-else>

        <template slot="title">

          <i :class="item.icon"></i>

          <span>{{ item.name }}</span>

        </template>

        <el-menu-item

          v-for="(child, ind) in item.children"

          :index="child.path"

          :key="ind"

          v-if="!child.meta.hidden"

        >

          <!-- <i :class="child.meta.icon"></i> -->

          <span slot="title">{{ child.name }}</span>

        </el-menu-item>

      </el-submenu>

    </div>

  </el-menu>

</template>

<script>

import { constantRoutes } from "@/router/index";

export default {

  name: "AsideContainer",

  data: () => {

    return {

      routeList: constantRoutes,

      currentIndex: constantRoutes[0].children[0].path,

    };

  },

  methods: {

    handleSelect(key, keyPath) {

      this.currentIndex = key;

      this.$router.push({ path: key });

    },

  },

  created() {

    // console.log(location);

    let index = location.pathname.indexOf("/detail");

    let indexC = location.pathname.indexOf("/compare");

    if (index !== -1) {

      this.$data.currentIndex = location.pathname.slice(0, index);

    } else if (indexC !== -1) {

      this.$data.currentIndex = location.pathname.slice(0, indexC);

    } else {

      this.$data.currentIndex = location.pathname;

    }

  },

};

</script>

:default-active="currentIndex"需要我们手动添加

:default-active="

      this.$route.path.indexOf('detail') == -1 ? this.$route.path : currentIndex

    "

全部代码

<template>

  <el-menu

    :default-active="

      this.$route.path.indexOf('detail') == -1 ? this.$route.path : currentIndex

    "

    @select="handleSelect"

  >

    <div v-for="(item, index) in routeList" :key="index">

      <template v-if="item.hidden"></template>

      <template

        v-else-if="

          !item.children ||

          !item.children.length ||

          (item.meta && item.meta.hideChildren)

        "

      >

        <el-menu-item :index="item.path">

          <i :class="item.icon"></i>

          <span slot="title">{{ item.name }}</span>

        </el-menu-item>

      </template>

      <el-submenu :index="item.path" v-else>

        <template slot="title">

          <i :class="item.icon"></i>

          <span>{{ item.name }}</span>

        </template>

        <el-menu-item

          v-for="(child, ind) in item.children"

          :index="child.path"

          :key="ind"

          v-if="!child.meta.hidden"

        >

          <!-- <i :class="child.meta.icon"></i> -->

          <span slot="title">{{ child.name }}</span>

        </el-menu-item>

      </el-submenu>

    </div>

  </el-menu>

</template>

<script>

import { constantRoutes } from "@/router/index";

export default {

  name: "AsideContainer",

  data: () => {

    return {

      routeList: constantRoutes,

      currentIndex: constantRoutes[0].children[0].path,

    };

  },

  methods: {

    handleSelect(key, keyPath) {

      this.currentIndex = key;

      this.$router.push({ path: key });

    },

    // activeIndex() {

    //   if (this.$route.matched.length === 2) {

    //     let parent = this.$route.matched[0];

    //     if (parent.meta && parent.meta.hideChildren) {

    //       this.$data.currentIndex = parent.path;

    //     } else {

    //       this.$data.currentIndex = this.$route.path;

    //     }

    //   }

    // },

  },

  created() {

    console.log(location);

    let index = location.pathname.indexOf("/detail");

    let indexC = location.pathname.indexOf("/compare");

    if (index !== -1) {

      this.$data.currentIndex = location.pathname.slice(0, index);

    } else if (indexC !== -1) {

      this.$data.currentIndex = location.pathname.slice(0, indexC);

    } else {

      this.$data.currentIndex = location.pathname;

    }

    // if (this.$route.matched.length > 2) {

    //   this.$data.currentIndex = this.$route.matched[1].path;

    // } else {

    //   // this.$data.currentIndex = this.$route.path;

    //   this.activeIndex();

    // }

  },

  watch: {

    $route(route) {

      // console.log("watch router: ", route);

      let index = route.path.indexOf("/detail");

      let indexC = route.path.indexOf("/compare");

      if (index !== -1) {

        this.$data.currentIndex = route.path.slice(0, index);

      } else if (indexC !== -1) {

        this.$data.currentIndex = route.path.slice(0, indexC);

      } else {

        this.$data.currentIndex = route.path;

      }

    },

  },

};

</script>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值