我的程式碼有問題,我不知道為什麼 s 不工作並且總是給出不同的錯誤 無論如何我嘗試過 錯誤: 'arraysorted' 未定義 no-undef
#<div> {{ arraysorted }} </div> </template> <script> const Array = []; export default { data: () => ({ Array: [1, 24, 23, 56, 76, 5, 468, 97, 65, 90, 23, 53, 23], arraysorted: [], }), mounted: { ArraySort() { return arraysorted = Array.sort(function (a, b) { return b - a; }); }, }, }; </script>
P粉3620719922023-09-07 09:58:55
您可以使用計算屬性:
new Vue({ el: "#demo", data: () => ({ myArray: [1, 24, 23, 56, 76, 5, 468, 97, 65, 90, 23, 53, 23], }), computed: { arraysorted() { const arrSorted = [...this.myArray] return arrSorted.sort((a, b) => b - a) }, }, })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="demo"> {{ arraysorted }} {{myArray}} </div>