9.3. 添加最热文章

本教程最新版为 2.6,当前版本已放弃维护,请阅读最新版本!

简介

本小节中,我们将添加七天内最热的文章,同时添加一些其他内容。

模拟最热文章

打开 src/mock/index.js 文件,在代码的最后面,添加模拟数据:

src/mock/index.js

.
.
.
// 拦截最热文章请求并返回相关数据,请求方法为 POST
Mock.mock('/articles/hot', 'post', (options) => {
  // 将评论最少的文章排在前面
  let filteredArticles = store.getters.getArticlesByFilter('noreply')
  // 将评论最多的文章排在前面
  let articles = filteredArticles.reverse()
  // 取 7 天内评论最多的文章
  let hotArticles = articles.filter((article) => (new Date() - new Date(article.date) < 604800000))
  // 文章条数
  let num

  // 请求有传 num 时使用它
  if (options.body) {
    try {
      num = JSON.parse(options.body).num
    } catch (e) {}
  }

  // 取前 num 条评论最多的文章,默认 10 条
  hotArticles = hotArticles.slice(0, num || 10)

  return hotArticles
})

我们指定被评论最多的文章为最热文章,上面的 ho...

本文章首发在 LearnKu.com 网站上。

为了保证课程的高品质,我们需要对课程进行收费。付费后 才能观看剩余内容。 购买

上一篇 下一篇
讨论数量: 0

暂无话题~