若依 ruoyi-vue 添加菜单路由搜索

效果图

在这里插入图片描述
在这里插入图片描述
若依前端有右上角搜索组件 src\components\HeaderSearch

动态配置

在这里插入图片描述
模仿原先前端配置
/src/layout/components/Settings

        <div class="drawer-item">
          <span>显示菜单搜索</span>
          <el-switch v-model="sidebarSearch" class="drawer-switch" />
        </div>

    sidebarLogo: {
      get() {
        return this.$store.state.settings.sidebarLogo
      },
      set(val) {
        this.$store.dispatch('settings/changeSetting', {
          key: 'sidebarLogo',
          value: val
        })
      }
    },
    sidebarSearch: {
      get() {
        return this.$store.state.settings.sidebarSearch
      },
      set(val) {
        this.$store.dispatch('settings/changeSetting', {
          key: 'sidebarSearch',
          value: val
        })
      }
    },

      this.$cache.local.set(
        "layout-setting",
        `{
            "topNav":${this.topNav},
            "tagsView":${this.tagsView},
            "fixedHeader":${this.fixedHeader},
            "sidebarLogo":${this.sidebarLogo},
            "sidebarSearch":${this.sidebarSearch},
            "dynamicTitle":${this.dynamicTitle},
            "sideTheme":"${this.sideTheme}",
            "theme":"${this.theme}"
          }`
      );

src/store/modules/settings.js

const { sideTheme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo,sidebarSearch, dynamicTitle } = defaultSettings


const state = {
  title: '',
  theme: storageSetting.theme || '#409EFF', // E60012主题颜色
  sideTheme: storageSetting.sideTheme || sideTheme,
  showSettings: showSettings,
  topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav,
  tagsView: storageSetting.tagsView === undefined ? tagsView : storageSetting.tagsView,
  fixedHeader: storageSetting.fixedHeader === undefined ? fixedHeader : storageSetting.fixedHeader,
  sidebarLogo: storageSetting.sidebarLogo === undefined ? sidebarLogo : storageSetting.sidebarLogo,
  sidebarSearch: storageSetting.sidebarSearch === undefined ? sidebarSearch : storageSetting.sidebarSearch,
  dynamicTitle: storageSetting.dynamicTitle === undefined ? dynamicTitle : storageSetting.dynamicTitle
}

页面中添加

src/layout/components/Sidebar/index.vue

<template>
    <div :class="{'has-logo':showLogo}" :style="{ backgroundColor: settings.sideTheme === 'theme-dark' ? variables.menuBackground : variables.menuLightBackground }">
        <logo v-if="showLogo" :collapse="isCollapse"/>
        <Search v-if="showSidebarSearch" :collapse="isCollapse" @searchOpen="searchOpen()"/>
        <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"
            >
                <sidebar-item
                    v-for="(route, index) in sidebarRouters"
                    :key="route.path  + index"
                    :item="route"
                    :base-path="route.path"
                />
            </el-menu>
        </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";
import Search from '@/components/NavBarSearch'

export default {
  components: {SidebarItem, Logo, Search},
    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;
        },
        showSidebarSearch() {
          return this.$store.state.settings.sidebarSearch;
        },
        variables() {
            return variables;
        },
        isCollapse() {
            return !this.sidebar.opened;
        }
    },
  methods: {
    searchOpen() {
      this.sidebar.opened = true
    }
  }
};
</script>

<template>
  <div :class="{'show':show}" class="header-search" :style="{ backgroundColor: sideTheme === 'theme-dark' ? variables.menuBackground : variables.menuLightBackground }">
    <el-select
      v-if="!collapse"
      ref="headerSearchSelect"
      v-model="search"
      :remote-method="querySearch"
      filterable
      default-first-option
      remote
      placeholder=""
      class="header-search-select"
      @change="change"
    >
      <el-option v-for="option in options" :key="option.item.path" :value="option.item" :label="option.item.title.join(' > ')" />
    </el-select>
    <svg-icon class-name="search-icon" icon-class="search" @click.stop="click" :style="{ color: sideTheme === 'theme-dark' ? variables.logoTitleColor : variables.logoLightTitleColor }" />
  </div>
</template>

<script>
// fuse is a lightweight fuzzy-search module
// make search results more in line with expectations
import Fuse from 'fuse.js/dist/fuse.min.js'
import path from 'path'
import variables from '@/assets/styles/variables.scss'
export default {
  name: 'NavBarSearch',
  props: {
    collapse: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      search: '',
      options: [],
      searchPool: [],
      show: true,
      fuse: undefined
    }
  },
  computed: {
    routes() {
      return this.$store.getters.permission_routes
    },
    variables() {
      return variables;
    },
    sideTheme() {
      return this.$store.state.settings.sideTheme
    }
  },
  watch: {
    routes() {
      this.searchPool = this.generateRoutes(this.routes)
    },
    searchPool(list) {
      this.initFuse(list)
    },
    show(value) {
      if (value) {
        document.body.addEventListener('click', this.close)
      } else {
        document.body.removeEventListener('click', this.close)
      }
    }
  },
  mounted() {
    this.searchPool = this.generateRoutes(this.routes)
  },
  methods: {
    click() {
      //菜单关闭状态点击图标,打开菜单
      if (this.collapse) {
        this.$emit('searchOpen')
      } else {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
      }
    },
    close() {
      this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
      this.options = []
    },
    change(val) {
      const path = val.path;
      const query = val.query;
      if(this.ishttp(val.path)) {
        // http(s):// 路径新窗口打开
        const pindex = path.indexOf("http");
        window.open(path.substr(pindex, path.length), "_blank");
      } else {
        if (query) {
          this.$router.push({ path: path, query: JSON.parse(query) });
        } else {
          this.$router.push(path)
        }
      }
      this.search = ''
      this.options = []
    },
    initFuse(list) {
      this.fuse = new Fuse(list, {
        shouldSort: true,
        threshold: 0.4,
        location: 0,
        distance: 100,
        minMatchCharLength: 1,
        keys: [{
          name: 'title',
          weight: 0.7
        }, {
          name: 'path',
          weight: 0.3
        }]
      })
    },
    // Filter out the routes that can be displayed in the sidebar
    // And generate the internationalized title
    generateRoutes(routes, basePath = '/', prefixTitle = []) {
      let res = []

      for (const router of routes) {
        // skip hidden router
        if (router.hidden) { continue }

        const data = {
          path: !this.ishttp(router.path) ? path.resolve(basePath, router.path) : router.path,
          title: [...prefixTitle]
        }

        if (router.meta && router.meta.title) {
          data.title = [...data.title, router.meta.title]

          if (router.redirect !== 'noRedirect') {
            // only push the routes with title
            // special case: need to exclude parent router without redirect
            res.push(data)
          }
        }

        if (router.query) {
          data.query = router.query
        }

        // recursive child routes
        if (router.children) {
          const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
          if (tempRoutes.length >= 1) {
            res = [...res, ...tempRoutes]
          }
        }
      }
      return res
    },
    querySearch(query) {
      if (query !== '') {
        this.options = this.fuse.search(query)
      } else {
        this.options = []
      }
    },
    ishttp(url) {
      return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
    }
  }
}
</script>

<style lang="scss" scoped>
.header-search {
  position: relative;
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  overflow: hidden;
  font-size: 0 !important;
  margin-bottom: 10px;

  .search-icon {
    margin-left: 16px;
    cursor: pointer;
    font-size: 14px;
    vertical-align: middle;
    color: #C3C7CD;
  }

  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

    ::v-deep .el-input__inner {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      color: #FFFFFF;
      border-bottom: 1px solid #C3C7CD;
      vertical-align: middle;
    }
  }

  &.show {
    .header-search-select {
      width: 180px;
      margin-left: 10px;
    }
  }
}
</style>

RuoYi-Vue是一个基于Vue.js的后台管理系统框架,它通常使用Vue Router来进行路由管理。配置菜单路由参数主要是为了组织页面结构和提供导航功能。以下是基本步骤: 1. 安装依赖:首先需要安装`vue-router`,如果还没有安装,可以在项目里运行 `npm install vue-router` 或者 `yarn add vue-router`。 2. 导入并配置Router:在main.js或单独的router文件中,导入Vue Router,并创建一个实例: ```javascript import Vue from 'vue' import Router from 'vue-router' // 引入菜单数据 import { menuList } from '@/constant/routerMenu' Vue.use(Router) const router = new Router({ routes: menuList.map(menu => ({ path: menu.path, name: menu.name, component: resolve => require(['@/views/' + menu.component], resolve), // 根据菜单组件名动态引入 meta: menu.meta || {}, // 添加自定义元信息 children: menu.children && menu.children.map(child => ({ path: child.path, name: child.name, component: resolve => require(['@/views/' + child.component], resolve), meta: child.meta || {} })) })), mode: 'history' // 如果使用HTML5 History模式,需设置此选项 }) ``` 3. 路由守卫:对于某些特殊的菜单,可能会有权限控制,这时可以使用路由守卫(如`beforeEach`)来检查用户是否有访问权限。 4. 更新UI:在Vue实例的生命周期钩子`mounted`或者某个视图的`created`中,可以根据当前的路由路径显示对应的菜单项高亮等效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值