当前位置:首页 > VUE

vue实现热门城市

2026-02-19 03:01:36VUE

实现热门城市功能

在Vue中实现热门城市功能,可以通过数据驱动的方式结合UI组件完成。以下是几种常见实现方式:

数据准备

定义热门城市数据数组,通常在组件的data或Vuex/store中管理:

data() {
  return {
    hotCities: [
      { id: 1, name: '北京' },
      { id: 2, name: '上海' },
      { id: 3, name: '广州' },
      { id: 4, name: '深圳' },
      { id: 5, name: '杭州' }
    ]
  }
}

基础列表渲染

使用v-for指令渲染城市列表:

<div class="hot-city-container">
  <div v-for="city in hotCities" :key="city.id" class="city-item">
    {{ city.name }}
  </div>
</div>

带交互的实现

添加点击事件处理:

methods: {
  handleCityClick(city) {
    this.$emit('city-selected', city)
    // 或执行路由跳转
    // this.$router.push(`/city/${city.id}`)
  }
}

对应模板:

<div 
  v-for="city in hotCities" 
  :key="city.id" 
  class="city-tag"
  @click="handleCityClick(city)">
  {{ city.name }}
</div>

样式优化

建议使用弹性布局实现网格效果:

.hot-city-container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.city-tag {
  padding: 8px 15px;
  background: #f5f5f5;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}
.city-tag:hover {
  background: #e0e0e0;
}

从后端获取数据

实际项目中通常从API获取数据:

async created() {
  try {
    const res = await axios.get('/api/hot-cities')
    this.hotCities = res.data
  } catch (error) {
    console.error('获取热门城市失败', error)
  }
}

组件化实现

对于复用场景,可封装为独立组件:

<!-- HotCities.vue -->
<template>
  <div class="hot-cities">
    <h3>热门城市</h3>
    <div class="city-list">
      <!-- 列表内容 -->
    </div>
  </div>
</template>

性能优化

对于大量城市数据,建议:

vue实现热门城市

  • 添加虚拟滚动
  • 实现分页加载
  • 使用keep-alive缓存组件

完整组件示例

<template>
  <div class="hot-cities">
    <h3 class="title">热门城市</h3>
    <div class="city-container">
      <span
        v-for="city in cities"
        :key="city.id"
        class="city-item"
        @click="selectCity(city)">
        {{ city.name }}
      </span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    cities: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    selectCity(city) {
      this.$emit('select', city)
    }
  }
}
</script>

<style scoped>
.hot-cities {
  margin: 20px 0;
}
.title {
  margin-bottom: 10px;
  font-size: 16px;
  color: #333;
}
.city-container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.city-item {
  padding: 6px 12px;
  background: #f0f0f0;
  border-radius: 4px;
  cursor: pointer;
}
.city-item:hover {
  background: #e0e0e0;
}
</style>

标签: 热门城市
分享给朋友:

相关文章

vue实现城市定位功能

vue实现城市定位功能

使用Geolocation API获取用户位置 在Vue中实现城市定位功能,可以通过浏览器提供的Geolocation API获取用户的经纬度坐标。需要在mounted生命周期钩子中调用相关方法。…

php热门如何实现

php热门如何实现

PHP 实现热门的常见方法 基于访问量统计 通过记录文章或页面的访问次数,按访问量降序排列。可使用数据库字段(如view_count)累加统计,定期刷新热门数据。 // 示例:更新访问量 $arti…

vue实现城市列表

vue实现城市列表

Vue 实现城市列表 数据结构设计 使用数组存储城市数据,每个城市对象包含 id、name 等字段: const cities = [ { id: 1, name: '北京' }, { id…

vue实现城市索引列表

vue实现城市索引列表

实现城市索引列表的步骤 数据准备 城市数据通常以数组形式存储,每个城市对象包含名称和拼音首字母。例如: const cities = [ { name: '北京', pinyin: 'bj' }…

用vue实现选择城市

用vue实现选择城市

实现选择城市功能 安装依赖 确保项目中已安装Vue及相关UI库(如Element UI或Vant)。以Element UI为例: npm install element-ui 基础组件结构 在Vue…

vue实现城市定位

vue实现城市定位

实现城市定位的方法 使用浏览器原生API获取地理位置 通过navigator.geolocation获取用户经纬度坐标,再调用逆地理编码服务转换为城市信息。 // 在Vue组件中 methods:…