当前位置:首页 > VUE

vue实现城市模糊查询

2026-02-23 18:36:17VUE

Vue 实现城市模糊查询

数据准备

准备城市数据列表,可以存储在组件的 data 中或通过 API 获取。例如:

data() {
  return {
    cities: ['北京', '上海', '广州', '深圳', '杭州', '南京', '成都', '重庆'],
    searchQuery: '',
    filteredCities: []
  }
}

监听输入变化

使用 v-model 绑定输入框的值,并通过 watchcomputed 监听输入变化:

watch: {
  searchQuery(newVal) {
    this.filterCities(newVal)
  }
}

实现模糊查询逻辑

通过字符串匹配或正则表达式实现模糊查询。以下是一个简单的实现:

vue实现城市模糊查询

methods: {
  filterCities(query) {
    if (!query) {
      this.filteredCities = this.cities
      return
    }
    this.filteredCities = this.cities.filter(city => 
      city.toLowerCase().includes(query.toLowerCase())
    )
  }
}

模板部分

在模板中添加输入框和结果列表:

<template>
  <div>
    <input v-model="searchQuery" placeholder="输入城市名称" />
    <ul>
      <li v-for="city in filteredCities" :key="city">
        {{ city }}
      </li>
    </ul>
  </div>
</template>

使用第三方库优化

可以使用 lodashdebounce 函数优化性能,避免频繁触发查询:

vue实现城市模糊查询

import { debounce } from 'lodash'

methods: {
  filterCities: debounce(function(query) {
    // 模糊查询逻辑
  }, 300)
}

高亮匹配部分

如果需要高亮显示匹配的部分,可以使用 v-html 和正则表达式替换:

methods: {
  highlightMatch(city, query) {
    const regex = new RegExp(query, 'gi')
    return city.replace(regex, match => `<span class="highlight">${match}</span>`)
  }
}
<li v-for="city in filteredCities" :key="city" v-html="highlightMatch(city, searchQuery)" />

样式优化

添加一些基础样式提升用户体验:

.highlight {
  background-color: yellow;
  font-weight: bold;
}

完整示例代码

以下是一个完整的 Vue 组件示例:

<template>
  <div>
    <input v-model="searchQuery" placeholder="输入城市名称" />
    <ul>
      <li v-for="city in filteredCities" :key="city" v-html="highlightMatch(city, searchQuery)" />
    </ul>
  </div>
</template>

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      cities: ['北京', '上海', '广州', '深圳', '杭州', '南京', '成都', '重庆'],
      searchQuery: '',
      filteredCities: []
    }
  },
  created() {
    this.filteredCities = this.cities
  },
  watch: {
    searchQuery(newVal) {
      this.filterCities(newVal)
    }
  },
  methods: {
    filterCities: debounce(function(query) {
      if (!query) {
        this.filteredCities = this.cities
        return
      }
      this.filteredCities = this.cities.filter(city => 
        city.toLowerCase().includes(query.toLowerCase())
      )
    }, 300),
    highlightMatch(city, query) {
      if (!query) return city
      const regex = new RegExp(query, 'gi')
      return city.replace(regex, match => `<span class="highlight">${match}</span>`)
    }
  }
}
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

标签: 模糊城市
分享给朋友:

相关文章

实现模糊查询vue

实现模糊查询vue

实现模糊查询的方法 在Vue中实现模糊查询通常需要结合输入框和列表渲染,通过监听输入框的变化实时过滤数据。以下是几种常见的实现方式: 使用计算属性过滤数据 <template> &l…

vue如何实现模糊查询

vue如何实现模糊查询

实现模糊查询的方法 在Vue中实现模糊查询通常需要结合前端数据处理或后端API调用。以下是几种常见方法: 使用JavaScript的filter和includes方法 // 假设这是你的数据源…

vue实现input模糊查询

vue实现input模糊查询

实现 Vue 的 input 模糊查询 在 Vue 中实现 input 模糊查询,可以通过监听输入框的变化,实时过滤数据列表。以下是一个完整的实现方法: 模板部分 <template&…

tree模糊搜索实现vue

tree模糊搜索实现vue

实现 Vue 中的 Tree 模糊搜索 在 Vue 中实现 Tree 组件的模糊搜索功能,通常需要结合递归组件、过滤算法和动态渲染。以下是具体实现方法: 递归组件结构 定义一个递归的 Tree 组…

vue实现前端模糊查询

vue实现前端模糊查询

实现前端模糊查询的方法 在Vue中实现前端模糊查询可以通过多种方式完成,以下介绍几种常见的方法: 使用JavaScript的filter和includes方法 定义一个搜索方法,利用数组的filte…

vue实现城市模糊查询

vue实现城市模糊查询

实现思路 使用Vue实现城市模糊查询功能,主要通过输入框监听用户输入,结合数组的过滤方法动态匹配城市名称。核心步骤包括数据准备、输入监听、模糊匹配和结果展示。 数据准备 准备一个城市列表数据,可以存…