I am new to Vue. I struggling and trying last half day not got any solution. Here, I need to change todos text automatically based ajax response. Using setInterval need to update vue instance and change HTML DOM as well. When I update todo object, can't change the DOM automatically
<div id="app">
<ul>
<li v-for="question in todos.text">
{{ question.text }}
</li>
</ul>
</div>
<script>
var app = new Vue({
el: '#app',
data: function () {
return {
message: 'You loaded this page on ' + new Date().toLocaleString(),
todos:
{
Event: 'Event1',
text: [
{ text: 'Learn JavaScript1' },
{ text: 'Learn Vue1' },
{ text: 'Build something awesome1' }
]
}
}
},
mounted: function() {
setInterval(function () {
axios({
method: 'post',
url: 'test.php',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
}).then(response => {
console.log(response.data);
this.todos = response.data;
Vue.set(this, todos, response.data );
})
.catch(err => {
console.log(err);
});
}, 5000);
}
})
</script>
thisis referencing yoursetInternval()scope, which isWindowrather than the Vue instance.