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

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

好的,以下是代码示例: ``` <template> <div> <el-menu :default-active="$route.path" class="el-menu-vertical-demo" @select="handleMenuSelect" :collapse="isCollapse" router > <el-submenu v-for="item in menuData" :key="item.id" :index="item.path"> <template slot="title"> <i :class="item.icon"></i> <span>{{ item.title }}</span> </template> <el-menu-item v-for="child in item.children" :key="child.id" :index="child.path"> {{ child.title }} </el-menu-item> </el-submenu> </el-menu> <el-button type="primary" @click="toggleCollapse">{{ isCollapse ? '展开' : '收起' }}</el-button> </div> </template> <script> export default { data() { return { menuData: [ { id: 1, path: '/dashboard', title: 'Dashboard', icon: 'el-icon-menu', children: [ { id: 11, path: '/dashboard/overview', title: '概览' }, { id: 12, path: '/dashboard/analysis', title: '分析' } ] }, { id: 2, path: '/form', title: '表单页', icon: 'el-icon-document', children: [ { id: 21, path: '/form/basic', title: '基础表单' }, { id: 22, path: '/form/advanced', title: '高级表单' } ] }, { id: 3, path: '/list', title: '列表页', icon: 'el-icon-tickets', children: [ { id: 31, path: '/list/basic', title: '基础列表' }, { id: 32, path: '/list/card', title: '卡片列表' } ] } ], isCollapse: false } }, methods: { handleMenuSelect(index) { this.$router.push(index) }, toggleCollapse() { this.isCollapse = !this.isCollapse } } } </script> ``` 这里使用了Element UI的Menu和Submenu组件来实现导航菜单,使用Vue Router实现路由跳转。当点击菜单项时,会触发handleMenuSelect方法,通过this.$router.push来跳转到对应的路由。同时,切换侧边栏的展开和收起状态,可以通过toggleCollapse方法来实现。在刷新页面时,可以使用Vuex或者localStorage等方式来保存菜单数据,以便在重新加载页面时恢复侧边栏菜单的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值