当前位置:首页 > VUE

vue实现菜单搜索

2026-02-17 01:30:42VUE

实现菜单搜索功能

在Vue中实现菜单搜索功能需要结合数据过滤和动态渲染。以下是具体实现方法:

基础实现方案

数据准备

data() {
  return {
    menuItems: [
      { id: 1, name: '首页', path: '/' },
      { id: 2, name: '产品', path: '/products' },
      { id: 3, name: '服务', path: '/services' },
      { id: 4, name: '关于我们', path: '/about' }
    ],
    searchQuery: ''
  }
}

模板部分

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索菜单..." />
    <ul>
      <li v-for="item in filteredMenu" :key="item.id">
        <a :href="item.path">{{ item.name }}</a>
      </li>
    </ul>
  </div>
</template>

计算属性过滤

computed: {
  filteredMenu() {
    return this.menuItems.filter(item => 
      item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
    )
  }
}

高级实现方案(支持嵌套菜单)

数据结构调整

data() {
  return {
    menuItems: [
      {
        id: 1,
        name: '产品',
        children: [
          { id: 11, name: '手机' },
          { id: 12, name: '电脑' }
        ]
      },
      {
        id: 2,
        name: '服务',
        children: [
          { id: 21, name: '技术支持' },
          { id: 22, name: '咨询服务' }
        ]
      }
    ],
    searchQuery: ''
  }
}

递归过滤方法

methods: {
  filterMenu(items) {
    return items.filter(item => {
      if (item.children) {
        const filteredChildren = this.filterMenu(item.children)
        return (
          item.name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
          filteredChildren.length > 0
        )
      }
      return item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
    })
  }
}

计算属性调整

computed: {
  filteredMenu() {
    return this.filterMenu(this.menuItems)
  }
}

性能优化建议

防抖处理

import { debounce } from 'lodash'

methods: {
  handleSearch: debounce(function(query) {
    this.searchQuery = query
  }, 300)
}

模板调整

<input @input="e => handleSearch(e.target.value)" placeholder="搜索菜单..." />

样式增强

添加搜索状态样式

.search-highlight {
  background-color: yellow;
  font-weight: bold;
}

高亮匹配文本

methods: {
  highlightText(text) {
    if (!this.searchQuery) return text
    const regex = new RegExp(this.searchQuery, 'gi')
    return text.replace(regex, match => `<span class="search-highlight">${match}</span>`)
  }
}

模板使用

vue实现菜单搜索

<li v-for="item in filteredMenu" :key="item.id">
  <a :href="item.path" v-html="highlightText(item.name)"></a>
</li>

完整组件示例

<template>
  <div class="menu-search">
    <input 
      v-model="searchQuery" 
      placeholder="搜索菜单..." 
      class="search-input"
    />
    <ul class="menu-list">
      <li 
        v-for="item in filteredMenu" 
        :key="item.id" 
        class="menu-item"
      >
        <a 
          :href="item.path" 
          v-html="highlightText(item.name)"
          class="menu-link"
        ></a>
        <ul v-if="item.children" class="submenu">
          <li 
            v-for="child in item.children" 
            :key="child.id"
            class="submenu-item"
          >
            <a 
              :href="child.path" 
              v-html="highlightText(child.name)"
              class="submenu-link"
            ></a>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      menuItems: [
        // 菜单数据
      ],
      searchQuery: ''
    }
  },
  computed: {
    filteredMenu() {
      return this.filterMenu(this.menuItems)
    }
  },
  methods: {
    filterMenu(items) {
      // 递归过滤方法
    },
    highlightText(text) {
      // 高亮文本方法
    },
    handleSearch: debounce(function(query) {
      this.searchQuery = query
    }, 300)
  }
}
</script>

<style>
/* 样式定义 */
</style>

以上方案可根据实际需求进行调整,支持平级菜单和嵌套菜单的搜索功能,并提供了性能优化和用户体验增强的建议。

标签: 菜单vue
分享给朋友:

相关文章

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…

vue遮罩实现

vue遮罩实现

实现 Vue 遮罩的常见方法 使用 CSS 定位和透明度 创建一个全屏遮罩层,通过 CSS 固定定位覆盖整个视口,并设置半透明背景色。 <template> <div cl…

vue实现反馈

vue实现反馈

Vue 实现反馈功能的方法 在 Vue 中实现反馈功能可以通过多种方式,包括弹窗提示、Toast 消息、表单提交等。以下是几种常见的实现方法。 弹窗反馈 使用 Vue 的组件化特性创建一个弹窗组件,…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…

vue实现梯形

vue实现梯形

在Vue中实现梯形效果,可以通过CSS的transform属性或clip-path属性来实现。以下是几种常见的实现方法: 使用CSS transform实现梯形 通过CSS的transform: s…