当前位置:首页 > VUE

vue实现全局菜单搜索

2026-02-24 04:23:30VUE

实现全局菜单搜索功能

在Vue中实现全局菜单搜索功能需要结合路由配置和搜索逻辑。以下是具体实现方法:

创建搜索组件

新建一个Search.vue组件作为全局搜索入口:

<template>
  <div class="search-container">
    <input 
      v-model="searchQuery" 
      placeholder="搜索菜单..." 
      @input="handleSearch"
    />
    <ul v-if="filteredRoutes.length > 0">
      <li v-for="route in filteredRoutes" :key="route.path">
        <router-link :to="route.path">{{ route.meta.title }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      filteredRoutes: []
    }
  },
  methods: {
    handleSearch() {
      const query = this.searchQuery.toLowerCase()
      this.filteredRoutes = this.$router.options.routes.filter(route => {
        return route.meta?.title?.toLowerCase().includes(query)
      })
    }
  }
}
</script>

注册为全局组件

在main.js中全局注册搜索组件:

import Search from '@/components/Search.vue'

const app = createApp(App)
app.component('GlobalSearch', Search)

添加路由元信息

在路由配置中添加meta信息以便搜索:

const routes = [
  {
    path: '/dashboard',
    component: Dashboard,
    meta: { title: '控制面板' }
  },
  {
    path: '/users',
    component: Users,
    meta: { title: '用户管理' }
  }
]

实现快捷键触发

添加键盘事件监听触发搜索:

mounted() {
  window.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.key === 'k') {
      this.$refs.searchInput.focus()
    }
  })
}

样式优化

为搜索组件添加基本样式:

.search-container {
  position: relative;
}

.search-container input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.search-container ul {
  position: absolute;
  width: 100%;
  background: white;
  border: 1px solid #eee;
  max-height: 300px;
  overflow-y: auto;
}

嵌套路由处理

对于嵌套路由,需要递归处理:

function flattenRoutes(routes) {
  return routes.reduce((acc, route) => {
    if (route.children) {
      acc = acc.concat(flattenRoutes(route.children))
    }
    return acc.concat(route)
  }, [])
}

性能优化

添加防抖减少频繁搜索:

import { debounce } from 'lodash'

methods: {
  handleSearch: debounce(function() {
    // 搜索逻辑
  }, 300)
}

多语言支持

考虑国际化场景:

vue实现全局菜单搜索

handleSearch() {
  const currentLocale = this.$i18n.locale
  this.filteredRoutes = this.$router.options.routes.filter(route => {
    const title = route.meta?.title?.[currentLocale] || route.meta?.title
    return title?.toLowerCase().includes(this.searchQuery.toLowerCase())
  })
}

以上实现方案提供了完整的全局菜单搜索功能,可根据实际项目需求进行调整和扩展。

标签: 全局菜单
分享给朋友:

相关文章

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue实现悬浮菜单

vue实现悬浮菜单

实现悬浮菜单的基本思路 使用Vue实现悬浮菜单通常需要结合CSS定位和Vue的事件监听。核心是通过v-show或v-if控制菜单显隐,通过鼠标事件触发状态变化。 HTML结构示例 <templ…

vue菜单怎么实现

vue菜单怎么实现

实现Vue菜单的基本方法 在Vue中实现菜单功能可以通过多种方式完成,以下是几种常见的方法: 使用v-for动态渲染菜单项 通过数据驱动的方式生成菜单,便于维护和扩展。定义菜单数据数组,利用v-f…

vue 实现动态菜单

vue 实现动态菜单

动态菜单的实现思路 在Vue中实现动态菜单通常涉及从后端API获取菜单数据,根据用户权限或角色动态渲染菜单项。核心思路是将菜单数据与组件分离,通过数据驱动视图。 数据准备与API交互 创建菜单数据模…

vue实现权限菜单

vue实现权限菜单

Vue实现权限菜单的方法 动态路由配置 通过后端返回的权限菜单数据动态生成路由配置。使用vue-router的addRoutes方法动态添加路由。 // 示例:动态添加路由 const router…

vue底部菜单实现

vue底部菜单实现

实现底部菜单的方法 在Vue中实现底部菜单通常涉及使用v-tab组件或自定义布局。以下是几种常见的方法: 使用Vuetify的v-bottom-navigation组件 Vuetify提供了专门的…