当前位置:首页 > VUE

vue实现菜单搜索

2026-01-16 00:14:39VUE

实现思路

在Vue中实现菜单搜索功能,通常需要结合输入框的实时监听、菜单数据的过滤以及结果的动态展示。核心逻辑包括监听用户输入、过滤菜单数据、高亮匹配关键词。

基本实现步骤

创建搜索输入框 在模板中添加一个输入框组件,用于接收用户输入的关键词。使用v-model双向绑定搜索关键词。

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

定义菜单数据和搜索关键词 在Vue的datasetup中定义菜单数据和搜索关键词的响应式变量。

data() {
  return {
    searchQuery: '',
    menuItems: [
      { id: 1, name: '首页' },
      { id: 2, name: '产品列表' },
      { id: 3, name: '关于我们' }
    ]
  }
}

计算过滤后的菜单 使用computed属性根据搜索关键词动态过滤菜单项。过滤逻辑通常包括不区分大小写和部分匹配。

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

高级功能扩展

添加防抖优化 频繁输入可能导致性能问题,可以通过防抖函数优化搜索触发频率。

methods: {
  debounceSearch: _.debounce(function() {
    this.filteredMenu = this.menuItems.filter(item => 
      item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
    )
  }, 300)
}

高亮匹配文本 在渲染菜单时,使用正则表达式将匹配部分包裹在<span>标签中并添加高亮样式。

vue实现菜单搜索

<li v-for="item in filteredMenu" :key="item.id" v-html="highlight(item.name)">
</li>
methods: {
  highlight(text) {
    const query = this.searchQuery
    if (!query) return text
    return text.replace(new RegExp(query, 'gi'), match => 
      `<span class="highlight">${match}</span>`
    )
  }
}

完整示例代码

<template>
  <div>
    <input 
      v-model="searchQuery" 
      @input="debounceSearch"
      placeholder="搜索菜单..."
    />
    <ul>
      <li 
        v-for="item in filteredMenu" 
        :key="item.id" 
        v-html="highlight(item.name)"
      ></li>
    </ul>
  </div>
</template>

<script>
import _ from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      menuItems: [
        { id: 1, name: '首页' },
        { id: 2, name: '产品列表' },
        { id: 3, name: '关于我们' }
      ],
      filteredMenu: []
    }
  },
  created() {
    this.filteredMenu = this.menuItems
  },
  methods: {
    debounceSearch: _.debounce(function() {
      this.filteredMenu = this.menuItems.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }, 300),
    highlight(text) {
      const query = this.searchQuery
      if (!query) return text
      return text.replace(new RegExp(query, 'gi'), match => 
        `<span class="highlight">${match}</span>`
      )
    }
  }
}
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

注意事项

  • 对于大型菜单数据,考虑使用Web Worker进行后台过滤以避免界面卡顿
  • 如果菜单数据来自API,需要在获取数据后初始化过滤逻辑
  • 移动端需要优化输入体验,可以考虑增加清除按钮和搜索图标

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

相关文章

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue实现计算

vue实现计算

Vue 实现计算的方法 在 Vue 中实现计算功能可以通过多种方式完成,主要包括计算属性(computed)、方法(methods)和侦听器(watch)。以下是具体的实现方法: 计算属性(Comp…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…