当前位置:首页 > VUE

vue实现搜索栏目

2026-02-18 01:43:06VUE

实现搜索栏的基本结构

在Vue中实现搜索栏,通常需要结合v-model进行数据双向绑定,监听用户输入并触发搜索逻辑。以下是一个基础模板:

<template>
  <div class="search-bar">
    <input 
      type="text" 
      v-model="searchQuery" 
      placeholder="输入关键词搜索"
      @keyup.enter="handleSearch"
    />
    <button @click="handleSearch">搜索</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: ''
    }
  },
  methods: {
    handleSearch() {
      this.$emit('search', this.searchQuery);
    }
  }
}
</script>

<style scoped>
.search-bar {
  display: flex;
  gap: 8px;
}
input {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
</style>

实现防抖优化

高频输入场景下,可以通过防抖(debounce)减少不必要的搜索请求:

import { debounce } from 'lodash';

export default {
  data() {
    return {
      searchQuery: '',
      debouncedSearch: null
    }
  },
  created() {
    this.debouncedSearch = debounce(this.emitSearch, 500);
  },
  methods: {
    emitSearch() {
      this.$emit('search', this.searchQuery);
    },
    handleSearch() {
      this.debouncedSearch();
    }
  }
}

结合后端API搜索

实际项目中通常需要调用API获取搜索结果:

vue实现搜索栏目

methods: {
  async handleSearch() {
    try {
      const response = await axios.get('/api/search', {
        params: { q: this.searchQuery }
      });
      this.$emit('results', response.data);
    } catch (error) {
      console.error('搜索失败:', error);
    }
  }
}

添加搜索建议

实现输入时的自动补全建议功能:

<template>
  <div class="search-container">
    <input 
      v-model="searchQuery" 
      @input="fetchSuggestions"
    />
    <ul v-if="suggestions.length" class="suggestions">
      <li 
        v-for="item in suggestions" 
        @click="selectSuggestion(item)"
      >
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      suggestions: []
    }
  },
  methods: {
    async fetchSuggestions() {
      if (this.searchQuery.length > 2) {
        const res = await axios.get('/api/suggest', {
          params: { q: this.searchQuery }
        });
        this.suggestions = res.data;
      }
    },
    selectSuggestion(item) {
      this.searchQuery = item;
      this.suggestions = [];
      this.handleSearch();
    }
  }
}
</script>

集成Vuex状态管理

大型项目建议使用Vuex集中管理搜索状态:

vue实现搜索栏目

// store/modules/search.js
export default {
  state: {
    query: '',
    results: []
  },
  mutations: {
    SET_QUERY(state, payload) {
      state.query = payload;
    },
    SET_RESULTS(state, payload) {
      state.results = payload;
    }
  },
  actions: {
    async search({ commit }, query) {
      commit('SET_QUERY', query);
      const res = await api.search(query);
      commit('SET_RESULTS', res.data);
    }
  }
}

组件中调用:

methods: {
  handleSearch() {
    this.$store.dispatch('search/search', this.searchQuery);
  }
},
computed: {
  results() {
    return this.$store.state.search.results;
  }
}

样式增强示例

添加更美观的搜索栏样式:

.search-bar {
  position: relative;
  max-width: 500px;
  margin: 0 auto;
}
input {
  width: 100%;
  padding: 12px 20px;
  border: 2px solid #ddd;
  border-radius: 30px;
  outline: none;
  transition: all 0.3s;
}
input:focus {
  border-color: #4285f4;
}
button {
  position: absolute;
  right: 5px;
  top: 5px;
  background: #4285f4;
  color: white;
  border: none;
  border-radius: 25px;
  padding: 7px 15px;
  cursor: pointer;
}

标签: 栏目vue
分享给朋友:

相关文章

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted 和…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="app"…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue实现阻塞

vue实现阻塞

Vue 实现阻塞的方法 在 Vue 中实现阻塞操作通常涉及异步控制、状态管理或生命周期钩子的使用。以下是几种常见方法: 使用 async/await 处理异步阻塞 通过 async/await 可…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…

vue框架实现CRM

vue框架实现CRM

Vue框架实现CRM系统 使用Vue框架构建CRM(客户关系管理)系统需要结合前端技术栈和后端API。以下为关键实现方法: 技术选型 核心框架:Vue 3(Composition API)…