当前位置:首页 > 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库:

vue实现搜索框检索

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>

高亮匹配结果

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

vue实现搜索框检索

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 Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router后…

vue实现一个系统

vue实现一个系统

Vue 系统实现步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目: npm init vue@latest my-system 选择需要的配置(Router、Pinia 等)。…

vue 实现弹幕

vue 实现弹幕

vue 实现弹幕的方法 使用 CSS 动画和动态渲染 在 Vue 中实现弹幕效果,可以通过动态渲染弹幕元素并结合 CSS 动画实现。以下是具体步骤: 数据准备 创建一个数组存储弹幕数据,每条弹幕包…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…