当前位置:首页 > VUE

vue实现热门文章查询

2026-02-22 03:34:03VUE

实现热门文章查询功能

在Vue中实现热门文章查询功能,通常需要结合前端展示和后端数据接口。以下是几种常见的实现方式:

前端展示组件

<template>
  <div class="hot-articles">
    <h3>热门文章</h3>
    <ul>
      <li v-for="article in hotArticles" :key="article.id">
        <router-link :to="`/article/${article.id}`">{{ article.title }}</router-link>
        <span class="views">浏览: {{ article.views }}</span>
      </li>
    </ul>
  </div>
</template>

数据获取方式

  1. 直接调用API获取

    <script>
    export default {
    data() {
     return {
       hotArticles: []
     }
    },
    async created() {
     const response = await fetch('/api/articles/hot')
     this.hotArticles = await response.json()
    }
    }
    </script>
  2. 使用Vuex状态管理

    
    <script>
    import { mapState } from 'vuex'

export default { computed: { ...mapState(['hotArticles']) }, created() { this.$store.dispatch('fetchHotArticles') } }

```

后端API实现思路

  1. 数据库查询热门文章

    // Express示例
    router.get('/api/articles/hot', async (req, res) => {
    const articles = await Article.find()
     .sort({ views: -1 })
     .limit(10)
    res.json(articles)
    })
  2. 使用Redis缓存热门数据

    // 使用Redis有序集合存储文章热度
    router.get('/api/articles/hot', async (req, res) => {
    const articleIds = await redis.zrevrange('article:hot', 0, 9)
    const articles = await Article.find({ _id: { $in: articleIds } })
    res.json(articles)
    })

热门度计算策略

  1. 简单浏览计数
    
    // 文章模型增加views字段
    const articleSchema = new Schema({
    views: { type: Number, default: 0 }
    })

// 阅读时增加计数 router.get('/api/article/:id', async (req, res) => { await Article.updateOne( { _id: req.params.id }, { $inc: { views: 1 } } ) // ...返回文章内容 })


2. 综合热度算法
```javascript
// 综合浏览量、点赞数、发布时间计算热度
function calculateHotScore(views, likes, createdAt) {
  const age = (Date.now() - createdAt) / (1000 * 3600 * 24)
  return (views * 1 + likes * 2) / Math.log(age + 2)
}

优化建议

  • 添加加载状态和错误处理

    <template>
    <div v-if="loading">加载中...</div>
    <div v-else-if="error">加载失败</div>
    <div v-else class="hot-articles">...</div>
    </template>
  • 实现自动刷新

    setInterval(async () => {
    this.hotArticles = await fetchHotArticles()
    }, 300000) // 每5分钟刷新一次
  • 添加缓存控制

    vue实现热门文章查询

    // 使用keep-alive缓存组件
    <keep-alive>
    <hot-articles />
    </keep-alive>

以上方案可根据实际项目需求进行组合和调整。对于大型应用,建议采用Vuex管理状态并结合Redis缓存热门数据。小型项目可以直接调用API获取数据并在前端展示。

标签: 热门文章vue
分享给朋友:

相关文章

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…

vue实现插件

vue实现插件

Vue 插件实现方法 Vue 插件通常用于为 Vue 应用添加全局功能或共享功能。以下是实现 Vue 插件的核心步骤: 插件基本结构 Vue 插件需要暴露一个 install 方法,该方法接收 Vu…

vue实现选择框

vue实现选择框

Vue 实现选择框的方法 Vue 中可以通过多种方式实现选择框(下拉框),包括原生 HTML 的 <select> 元素结合 Vue 的数据绑定,或者使用第三方 UI 库如 Element…