当前位置:首页 > VUE

vue实现搜索框检索

2026-01-22 09:39:43VUE

实现基础搜索框

在Vue中创建一个基础搜索框,使用v-model双向绑定输入值,通过计算属性或方法过滤数据:

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ]
    }
  },
  computed: {
    filteredList() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

添加防抖优化

为防止频繁触发搜索,可使用lodash的debounce方法或自定义防抖函数:

import { debounce } from 'lodash'

export default {
  methods: {
    search: debounce(function() {
      // 实际搜索逻辑
      this.filteredList = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }, 300)
  }
}

远程数据搜索

当需要从API获取搜索结果时,可使用axios等HTTP库:

export default {
  methods: {
    async searchRemote() {
      try {
        const response = await axios.get('/api/search', {
          params: { q: this.searchQuery }
        })
        this.results = response.data
      } catch (error) {
        console.error(error)
      }
    }
  }
}

添加搜索建议

实现自动完成功能,在用户输入时显示搜索建议:

<template>
  <div>
    <input 
      v-model="searchQuery" 
      @input="showSuggestions"
      @focus="showSuggestions"
      @blur="hideSuggestions"
    />
    <ul v-if="showSuggestionList">
      <li 
        v-for="suggestion in suggestions" 
        :key="suggestion.id"
        @click="selectSuggestion(suggestion)"
      >
        {{ suggestion.text }}
      </li>
    </ul>
  </div>
</template>

高亮匹配结果

在搜索结果中高亮显示匹配的文本部分:

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

多条件搜索

实现基于多个字段的复合搜索:

filteredList() {
  return this.items.filter(item => {
    const query = this.searchQuery.toLowerCase()
    return (
      item.name.toLowerCase().includes(query) ||
      item.description.toLowerCase().includes(query) ||
      item.category.toLowerCase().includes(query)
    )
  })
}

路由集成

将搜索参数同步到URL路由中,支持浏览器前进后退:

watch: {
  searchQuery(newVal) {
    this.$router.push({ query: { q: newVal } })
  }
},
created() {
  if (this.$route.query.q) {
    this.searchQuery = this.$route.query.q
  }
}

vue实现搜索框检索

标签: vue
分享给朋友:

相关文章

vue实现文档导入

vue实现文档导入

Vue 实现文档导入的方法 在 Vue 中实现文档导入功能通常涉及文件上传、解析和处理。以下是几种常见的方法: 使用原生文件输入和 FileReader 通过 HTML 的原生 <input…

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: n…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 me…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export defaul…

vue首页实现

vue首页实现

实现Vue首页的基本步骤 创建一个Vue首页通常涉及项目初始化、页面结构设计、路由配置和组件开发。以下是具体实现方法: 初始化Vue项目 使用Vue CLI或Vite快速搭建项目结构: npm i…