当前位置:首页 > 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>标签中并添加高亮样式。

<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 实现登录验证的方法 使用表单验证库 VeeValidate 安装 VeeValidate 库,可以快速实现表单验证功能。VeeValidate 提供了丰富的验证规则和错误提示功能。 npm…

vue实现多级组件

vue实现多级组件

实现多级组件的基本结构 在Vue中实现多级组件通常涉及父子组件嵌套,通过props向下传递数据,通过$emit向上传递事件。多级组件的核心是递归或动态组件渲染。 <!-- 父组件 Parent…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue 提供…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm insta…