I’m trying to accomplish the following but I don’t even know if it is even possible with Vue as I’m struggling to get the desired result:
I have an endpoint for an API which returns many objects within an array.
I am successfully rendering the data within my Vue application but I wanted to know if it is possible for Vue to “track” when the array has been updated with more objects and then render those in the view.
I am using setInterval
to perform a GET request every 10 minutes and the new data is going into the object within my data()
correctly but the changes are not reflected within the view.
At the moment I am changing a boolean from true
to false
at the beginning and end respectively so that the view is rendered again with v-if
.
My goal is to create a simple Twitter feed app that performs a GET request every 10 minutes, collects the tweets, puts them into my Vue instance and show them in the view without having to reload the page/re-render the component. Like an automatic Twitter feed that just constantly loads new tweets every 10 minutes.
Is this even possible? I’ve tried using the Vue.set()
method but that hasn’t made any difference.
If it’s not possible, what would be the best way to implement something similar?
Here is my code:
JavaScript:
new Vue({
el: '#app',
data: {
items: [],
},
created() {
this.load();
setInterval(() => this.load(), 5000);
},
methods: {
load() {
axios.get('https://reqres.in/api/users?page=2')
.then(response => {
this.items = response.data.data;
});
}
}
});
HTML
<div id="app">
<p v-for="item in items">
{{ item.first_name }}
</p>
</div>
CodePen: https://codepen.io/tomhartley97/pen/VwZpZNG
In the above code, if the array is updated by the GET request, the chances are not reflected within the view?
2
Answers
Yes it is possible. The way you need to set new reactive properties in your Vue instance is the following:
For Object properties:
Vue.set(this.baseObject, key, value)
The baseObject cannot be a Vue instance or the base data() object, so you will have to declare a container property.
For Array entries use native array methods: e.g.
Array.prototype.push()
.Using
Vue.set(array, arrayIndex, newArrayElement)
does not workHence, your solution might look something line that:
Well, I view the codepen, I known why your view do not get update: the api response always return the same array!
Try to return different data.
The api returns an array, so the
data
definesThe template may look like this
And the update methods