当前位置:首页 > VUE

vue实现商城搜索

2026-03-08 12:19:39VUE

Vue 实现商城搜索功能

搜索框组件设计

使用 Vue 的 v-model 实现双向数据绑定,监听用户输入。建议添加防抖处理(如 Lodash 的 _.debounce)避免频繁触发搜索。

<template>
  <div class="search-box">
    <input 
      v-model="keyword" 
      @keyup.enter="search" 
      placeholder="输入商品名称/品牌"
    />
    <button @click="search">搜索</button>
  </div>
</template>

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

搜索逻辑实现

通过 Vuex 或组件通信管理搜索状态,建议将搜索关键词作为路由参数实现可分享的 URL。

vue实现商城搜索

// 在页面组件中
methods: {
  handleSearch(keyword) {
    if (!keyword) return

    // 方式1:通过路由更新(推荐)
    this.$router.push({
      path: '/products',
      query: { q: keyword }
    })

    // 方式2:直接调用API
    this.fetchProducts({ keyword })
  },
  fetchProducts(params) {
    api.getProducts(params).then(res => {
      this.products = res.data.list
    })
  }
}

搜索结果展示

使用 v-for 渲染商品列表,建议添加加载状态和空状态提示。

vue实现商城搜索

<template>
  <div class="product-list">
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="!products.length" class="empty">暂无商品</div>
    <div v-else class="grid">
      <product-card 
        v-for="item in products" 
        :key="item.id" 
        :data="item"
      />
    </div>
  </div>
</template>

搜索建议功能

实现输入时的自动补全建议,需要与后端配合返回建议关键词。

watch: {
  keyword(newVal) {
    if (newVal) {
      this.fetchSuggestions(newVal)
    }
  }
},
methods: {
  fetchSuggestions: _.debounce(function(keyword) {
    api.getSuggestions(keyword).then(res => {
      this.suggestions = res.data
    })
  }, 300)
}

高级搜索筛选

添加价格区间、分类等筛选条件,使用对象管理筛选参数。

<div class="filters">
  <select v-model="filters.category">
    <option value="">全部分类</option>
    <option v-for="cat in categories" :value="cat.id">{{ cat.name }}</option>
  </select>
  <input v-model.number="filters.minPrice" placeholder="最低价">
  <input v-model.number="filters.maxPrice" placeholder="最高价">
</div>

性能优化建议

  1. 使用虚拟滚动(如 vue-virtual-scroller)处理大量商品列表
  2. 实现前端缓存已搜索过的结果
  3. 对图片使用懒加载(如 v-lazy)
  4. 分页加载(无限滚动或页码切换)
// 分页示例
data() {
  return {
    page: 1,
    pageSize: 20,
    hasMore: true
  }
},
methods: {
  loadMore() {
    if (!this.hasMore) return
    this.page++
    this.fetchProducts()
  },
  fetchProducts() {
    api.getProducts({
      keyword: this.keyword,
      page: this.page,
      pageSize: this.pageSize
    }).then(res => {
      this.products.push(...res.data.list)
      this.hasMore = res.data.hasMore
    })
  }
}

标签: 商城vue
分享给朋友:

相关文章

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue实现select

vue实现select

Vue 实现 Select 组件的方法 在 Vue 中实现 Select 组件可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML select 元素 原生 HTML 的 <sel…

vue实现框选

vue实现框选

Vue 实现框选功能 在 Vue 中实现框选功能通常需要监听鼠标事件,计算选区范围,并根据选区范围高亮或选中元素。以下是实现框选功能的关键步骤。 监听鼠标事件 在 Vue 模板中,为容器元素绑定鼠标…

vue逻辑实现怎么实现

vue逻辑实现怎么实现

Vue 逻辑实现方法 Vue 的逻辑实现主要通过组件化、响应式数据、计算属性、方法、生命周期钩子等方式完成。以下是一些核心实现方法: 数据驱动与响应式 在 Vue 中,通过 data 选项定义响应式…

vue select实现搜索

vue select实现搜索

实现 Vue Select 搜索功能的方法 在 Vue 中实现 select 组件的搜索功能,可以通过原生 HTML select 结合自定义搜索逻辑,或使用第三方库如 vue-select 来实现。…