我在尝试在 v-for 循环中按日期对数据数组进行排序时遇到问题。我尝试了 orderBy 但没成功。
这是简化的 v-for 循环:
<div v-for="article in articles">
<div class="article-date">{{ article.dateYear }}/{{ article.dateMonth }}</h1>
<h1>{{ article.title }}</h1>
<h3>{{ article.description }}</h3>
</div>
我的基本计算属性:
articles() {
return this.$store.state.articles;
}
每篇文章都有一个 dateDay、dateMonth 和 dateYear,所以我想做的是 date =article.dateYear +article.dateMonth +article.dateDay
并使用 date 变量和 orderBy,但它没有成功。
这是更好的方法吗?
感谢您的宝贵时间!
最佳答案
在文章帮助程序中,首先对文章进行排序:
articles() {
const { articles } = this.$store.state;
articles.sort(function(a,b){
return new Date(b.date) - new Date(a.date);
});
return articles;
}
关于javascript - v-for中按升序对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52624034/