javascript - Vuejs array push -
im receiving array of objects backend in below format. trying these datas , push javascript array can use them later on based on needs.
[ { id: 1, name: "dr. darrin frami iii", email: "gaylord67@example.com", address: "42568 cameron cove fritschborough, ma 86432-0749", }, ]
here vuejs code:
<script> export default { data(){ return { fakeusers: [], fakeuser: {id: '', name: '', email: ''}, } }, methods:{ }, mounted() { var route = '/get-users'; this.$http.get(route).then((response)=>{ (var = 0; < response.data.length; i++) { this.fakeuser.id = response.data[i].id; this.fakeuser.name = response.data[i].name; this.fakeuser.email = response.data[i].email; this.fakeusers.push(this.fakeuser); } }); console.log(this.fakeusers); console.log(this.fakeusers[0]); } } </script>
the vue-dev tool result:
output of line console.log(this.fakeusers);
[__ob__: observer]
. shouldnt print [array[10]]
??
output of lineconsole.log(this.fakeusers[0]);
undefinded
cant figure out why.. ? please help
$http() creates async ajax call, code in then()
executed after console command after it.
simple solution: put console commands function in .then()
well.
Comments
Post a Comment