当前位置:首页 > 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 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-t…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现单点

vue实现单点

Vue 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…