Answer the question
In order to leave comments, you need to log in
How to execute computed only after the function is executed?
The problem is that I am getting an error: Cannot read property 'april' of undefined"
The code is the following:
var app = new Vue({
el: '#app',
data: {
my_dates: {},
},
computed: {
myvalue: function () {
return Number(this.my_dates['2018-04-23']['april']) + Number(this.my_dates['2018-04-23']['may'])
}
},
methods:
{
getTableData: function()
{
// GET /someUrl
this.$http.get('http://127.0.0.1:8000/ponizovka_get_data').then(response => {
// get body data
this.my_dates = response.body;
}, response => {
// error callback
});
}
},
created: function(){
this.getTableData()
}
})
{{my_dates['2018-04-23']}}
works. my_dates['2018-04-23']['april']
it is not possible to contact. That is, it is displayed, but swears (see above). Answer the question
In order to leave comments, you need to log in
Question - how do you use myvalue? If somewhere in the template, then v-if will fix the situation:
Otherwise, it makes sense to check for the existence of the desired property in my_dates and act according to the circumstances, for example like this:
myvalue() {
сonst date = this.my_dates['2018-04-23'];
return date ? Number(date.april) + Number(date.may) : null;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question