Here's my data:
data: function(){
return {
contas: [{id: 3,
nome: "Conta de telefone",
pago: false,
valor: 55.99,
vencimento: "22/08/2016"}] //debug test value
};
},
And here's my get request:
beforeMount() {
axios.get('http://127.0.0.1/api/bills')
.then(function (response) {
console.log("before: " + this.contas);
this.contas = response.data;
console.log("after: " + this.contas);
});
},
The problem is I can't access this.contas
from within axios.get()
. I've tried Vue.set(this, 'contas', response.data);
and window.listaPagarComponent.contas = response.data;
without success.
My console shows:
before: undefined
after: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
But Vue Devtools shows only:
contas: Array[1]
0: Object
id: 3
nome: "Conta de telefone"
pago: false
valor: 55.99
vencimento: "22/08/2016"
Here's my full code.
created()
hook insteadbeforeMount()
.And if you already have defined some data in contas array, then I think into promise you should do array.push. – Handmadethis.contas
to refer to component's datacontas
. No function works. I think axios is a "object", so when I usethis
, it refers to axios. – Thiaminevar test = ''
. Then nothing changes. I thinkthis
is refering to axios somehow. Vue resource doesn't work with Vue.js 2 – ThiaminebeforeMount () {}
withmounted: function () {}
and the console shows the first log as[__ob__: Observer]
. Now I can push one of my objectsthis.contas.push(response.data[0]);
. But I can't set my object to be equal my response.data withthis.contas = response.data;
. Do I need to post another question? – Thiamineconsole.log(this)
inside of the promise, the result iswindow
, you need store thethis
reference in a auxiliar var and use the reference var to set the values. ....var that = this
;that.contas = response.data;
– Katelynnkaterina