【发布时间】:2017-08-21 02:10:52
【问题描述】:
我刚开始学习 VueJS 2,问题越多越难。
这个想法是我有一个对象数组的示例问题,我想通过“votes”属性对数组进行排序,该属性可以为每个单独的元素动态更新。我想动态地按投票对我的列表进行排序。因此,问题是如何在不编写奇怪代码的情况下做到这一点。
在角度你会做类似的事情
for candidate in candidates | orderBy: 'votes'
但在这里我只能想到类似的东西
v-for="(value, index, key) in sorted_candidates"
我将在 .js 中的哪个位置
computed : {
sorted_candidates() {
return this.candidates.sort((a, b) => { return b.votes - a.votes;});
}
}
所以我的问题是是否有更优雅的方法来解决这个问题? 注意:我正在对对象属性进行排序。
【问题讨论】:
-
使用计算是你在 Vue 2 中的做法。