I have encountered a strange behavior with Vue. I make an ajax call store the result (some JSON) to a Vue data property named: 'modello'.
{
"lineaGialla": {
"selected": false,
"descrizione": "Questa è la descrizione della linea gialla",
"servizi": [
{"nomeServizio": "servizio1","descrizione":"descrizione servizio1","selected": false},
{"nomeServizio": "servizio2","descrizione":"descrizione servizio2","selected": false},
{"nomeServizio": "servizio3","descrizione":"descrizione servizio3","selected": false}
]
}
}
In the template, when I access the data property lineaGialla using {{modello.lineaGialla}} it works, but when I try to access a nested property, such as {{modello.lineaGialla.descrizione}}, I get an error in the console:
[Vue warn]: Error in render function: "TypeError: Cannot read property 'descrizione' of undefined"
Here the ajax call.
var getData = function(){
return new Promise(function(resolve, reject){
$.ajax({
type:'get',
url:'https://api.myjson.com/bins/w9xlb',
dataType: 'json',
success: function(response){
resolve(response);
}
});
});
};
Here is my Vue.
var Metromappa = new Vue({
el: '#metromappa',
data: {
modello:{}
},
methods:{
},
mounted: function(){
var self = this;
getData().then(function(response){
self.modello = response;
}, function(error){
console.log(error);
});
}
})
What could it be?