当前位置:首页 > 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信息以便搜索:

vue实现全局菜单搜索

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()
    }
  })
}

样式优化

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

vue实现全局菜单搜索

.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)
}

多语言支持

考虑国际化场景:

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 elementui实现菜单

vue elementui实现菜单

Vue ElementUI 实现菜单 安装 ElementUI 确保项目已安装 Vue 和 ElementUI。若未安装,可以通过以下命令安装: npm install element-ui 在…

vue  全局刷新实现

vue 全局刷新实现

全局刷新实现方法 在Vue中实现全局刷新通常涉及重新加载整个应用或特定路由。以下是几种常见方法: 使用window.location.reload() 强制浏览器重新加载当前页面: windo…

vue实现导航菜单

vue实现导航菜单

Vue 实现导航菜单的方法 基础路由导航实现 在 Vue 项目中,使用 vue-router 可以轻松实现导航菜单功能。首先安装并配置路由: npm install vue-router 在 rou…

css制作下拉菜单

css制作下拉菜单

CSS 制作下拉菜单的方法 使用纯 CSS 可以创建简单的下拉菜单效果,主要依赖 :hover 伪类和 display 属性控制子菜单的显示与隐藏。 基础 HTML 结构 <div c…

如何制作css导航菜单

如何制作css导航菜单

创建基本HTML结构 导航菜单的基础是HTML结构。使用<nav>标签包裹无序列表<ul>,每个菜单项用<li>标签表示,链接用<a>标签。 <…

css制作菜单

css制作菜单

CSS 制作菜单的方法 使用 CSS 可以制作多种风格的菜单,包括水平菜单、垂直菜单、下拉菜单等。以下是几种常见的实现方式。 水平菜单 水平菜单通常用于导航栏,可以通过 display: inli…