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

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数组中,实现路由跳转动态的打开与之对应的菜单项

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

Logo

快速构建 Web 应用程序

更多推荐