若依vue框架+element ui 组件路由跳转与菜单联动

在后台管理系统中当点击某一按钮时,页面发生跳转(路由发生跳转,跳转到与按钮对应的页面),在跳转的同时在侧边栏中打开与之对应模块的菜单项

1.点击按钮跳转到/pay/PayIndex页面

2.在后台管理系统中侧边栏使用的是element ui 中的NavMenu导航菜单组件,在后台管理系统 src/layout/components/Sidebar/index中代码如下:

<template>
  <div
    :class="{ 'has-logo': showLogo }"
    :style="{
      backgroundColor:settings.sideTheme === 'theme-dark'? variables.menuBackground : variables.menuLightBackground,}">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar :class="settings.sideTheme" wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color=" settings.sideTheme === 'theme-dark' ? variables.menuBackground : variables.menuLightBackground"
        :text-color="settings.sideTheme === 'theme-dark' ? variables.menuColor: variables.menuLightColor"
        :unique-opened="true"
        :active-text-color="settings.theme"
        :collapse-transition="false"
        mode="vertical"
        :default-openeds="openeds"
      >
        <sidebar-item
          v-for="(route, index) in sidebarRouters"
          :key="route.path + index"
          :item="route"
          :base-path="route.path"
        />
      </el-menu>
      <!-- :index="route.path" -->
    </el-scrollbar>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import Logo from "./Logo";
import SidebarItem from "./SidebarItem";
import variables from "@/assets/styles/variables.scss";

export default {
  components: { SidebarItem, Logo },
  data() {
    return {
      openeds: [],
    };
  },
  computed: {
    ...mapState(["settings"]),
    ...mapGetters(["sidebarRouters", "sidebar"]),
    activeMenu() {
      const route = this.$route;
      const { meta, path } = route;
      // if set path, the sidebar will highlight the path you set
      if (meta.activeMenu) {
        return meta.activeMenu;
      }
      return path;
    },
    showLogo() {
      return this.$store.state.settings.sidebarLogo;
    },
    variables() {
      return variables;
    },
    isCollapse() {
      return !this.sidebar.opened;
    },
  },
  //组件路由发生变化,打开与组件对应模块的菜单项
  watch: {
    "$route.path": function (newVal) {
      // 先清空数组,确保数组中只存放一个路由信息
      if (this.openeds.length > 0) {
        this.openeds = [];
      }
      // newVal为路由 /pay/PayIndex 
      let str = newVal.split("/");
      // url为/pay  打开/pay所对应的菜单项
      let url = "/" + str[1];
      this.openeds.push(url);
    },
  },
};
</script>

可通过default-openeds指定想要打开的菜单项(也可设置默认打开的菜单项 如 :default-openeds="[/system/user]") 

 

 通过监听路由的变化,打开与要跳转的组件页面相对应的菜单项,把变化的路由存放到openeds数组中,实现路由跳转动态的打开与之对应的菜单项

以上就是实现路由跳转打开对应模块菜单项的所有步骤啦

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值