当前位置:首页 > VUE

vue实现搜索效果

2026-02-19 07:48:29VUE

Vue 实现搜索功能的方法

基础搜索实现

在 Vue 中实现搜索功能,可以通过 v-model 绑定输入框,结合计算属性过滤数据:

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用 Lodash 防抖

对于频繁触发的搜索,可以使用 Lodash 的防抖功能优化性能:

vue实现搜索效果

import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: [],
      allItems: []
    }
  },
  created() {
    this.debouncedSearch = debounce(this.doSearch, 300)
  },
  watch: {
    searchQuery(newVal) {
      this.debouncedSearch(newVal)
    }
  },
  methods: {
    doSearch(query) {
      this.items = this.allItems.filter(item =>
        item.name.toLowerCase().includes(query.toLowerCase())
      )
    }
  }
}

服务端搜索

对于大数据量搜索,建议使用服务端接口:

vue实现搜索效果

methods: {
  async searchItems() {
    try {
      const response = await axios.get('/api/items', {
        params: { q: this.searchQuery }
      })
      this.items = response.data
    } catch (error) {
      console.error('搜索出错:', error)
    }
  }
}

高级搜索组件

可以封装可复用的搜索组件:

<!-- SearchComponent.vue -->
<template>
  <div class="search-box">
    <input
      v-model="internalValue"
      @input="handleInput"
      placeholder="搜索..."
    />
    <button @click="$emit('search', internalValue)">搜索</button>
  </div>
</template>

<script>
export default {
  props: ['value'],
  data() {
    return {
      internalValue: this.value
    }
  },
  watch: {
    value(newVal) {
      this.internalValue = newVal
    }
  },
  methods: {
    handleInput() {
      this.$emit('input', this.internalValue)
    }
  }
}
</script>

使用 Vuex 管理搜索状态

在大型应用中,可以使用 Vuex 集中管理搜索状态:

// store.js
export default new Vuex.Store({
  state: {
    searchQuery: '',
    searchResults: []
  },
  mutations: {
    SET_SEARCH_QUERY(state, query) {
      state.searchQuery = query
    },
    SET_SEARCH_RESULTS(state, results) {
      state.searchResults = results
    }
  },
  actions: {
    async search({ commit }, query) {
      commit('SET_SEARCH_QUERY', query)
      const results = await api.search(query)
      commit('SET_SEARCH_RESULTS', results)
    }
  }
})

这些方法涵盖了从基础到高级的 Vue 搜索实现方式,可以根据项目需求选择合适的方案。

标签: 效果vue
分享给朋友:

相关文章

vue实现评论编辑

vue实现评论编辑

Vue 实现评论编辑功能 数据绑定与表单结构 使用 v-model 双向绑定评论内容到表单输入框,确保编辑时能显示原内容并同步修改。示例模板结构: <template> <di…

vue实现路由导航

vue实现路由导航

路由导航的实现方式 在Vue中,路由导航主要通过Vue Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router后…

vue实现高亮

vue实现高亮

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

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue实现幻灯

vue实现幻灯

Vue实现幻灯片的基础方法 使用Vue实现幻灯片功能可以通过多种方式完成,以下是几种常见方案: 方案一:基于Vue的过渡动画 通过Vue的<transition>或<transit…