9.3. 添加最热文章
简介
本小节中,我们将添加七天内最热的文章,同时添加一些其他内容。
模拟最热文章
打开 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...