vuejs reverse array error infinity loop code example
Example: vuejs reverse array error infinity loop
<!--
Array.prototype.reverse actually modifies the array it's applied to.
Vue picks up this change and triggers both v-for to re-evaluate, triggering another .reverse(). That triggers Vue to re-render, causing it to .reverse() etc etc etc etc etc etc etc etc etc etc...
To solve this, use a computed property on a shallow copy of items[] (e.g. using Array destructuring [...this.items] for the reversed list:
-->
<template>
<div id="app">
<ul v-for="item in items">
<li>{{ item }}</li>
</ul>
<hr />
<ul v-for="item in itemsReverse">
<li>{{ item }}</li>
</ul>
</div>
</template>
<script>
new Vue({
el: '#app',
data() {
return {
items: [1, 2, 3, 4]
}
},
computed: {
itemsReverse() {
return [...this.items].reverse()
}
}
})
</script>