Reputation: 3112
I´m trying to show a div when the api call is success. When i click a button and process is completed use v-if control to detect variable is true or false.
<div id="app" class="container h-100">
<div v-if="correct==true" class="alert alert-success" role="alert">
<strong>Correcto</strong>. Datos actualizados de forma satisfactoria.
</div>
<button v-on:click="guardar" type="button" class="btn btn-outline-primary btn-block">Modificar </button></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
var app = new Vue({
el: '#app',
data() {
return {
correct: false
}
},
methods: {
guardar: function () {
axios
.get('ENDPOINT')
.then(response => {
return {
correct: true
}
})
.catch(function (error) {
console.log(error);
})
},
}
})
</script>
The problem: V-if doesn´t detect when 'correct' variable is true. I suppose that something is wrong when i get response. What is the correct way to set 'correct' variable true when process is complete?
Upvotes: 0
Views: 1043
Reputation: 3908
You should change the state after a successful get request
<div v-if="correct" class="alert alert-success" role="alert">
<strong>Correcto</strong>. Datos actualizados de forma satisfactoria.
</div>
methods: {
guardar: function () {
axios
.get('ENDPOINT')
.then(response => {
// here update the correct state
this.correct = true
})
.catch(function (error) {
console.log(error)
this.correct = false
})
}
}
Upvotes: 1