I recently started using Vue.js, and have ran into a small problem. I have an array, and have used Vue.js to add that array into the rendered code. Using standard .push works fine for that.
However, I also want to be able to clear the array which would clear the rendered code. But when I try array = [] to clear it, the code doesn't work, and everything stops working.
How do I clear the list without breaking the program?
I replicated the problem in the below snippet.
let results = [1, 2];
let num = 3;
var app = new Vue({
el: '#app',
data: {
results: results
}
});
document.getElementById("add").addEventListener("click", function() {
results.push(num);
num++;
});
document.getElementById("clear").addEventListener("click", function() {
results = [];
num = 1;
});
.as-console-wrapper {
height: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<button id="add">Add</button>
<button id="clear">Clear</button>
<div id="app">
<h1 v-for="result in results"> {{ result }}</h1>
</div>