Reputation: 43
I have two v-for loops like so:
<div v-for="id in ids">
<div v-for="data in getData(id)">
<p>{{data.name}}</p>
</div>
</div>
The first v-for
iterates through an array of ids stored inside data()
.
On the second v-for
it iterates through the result returned by the function getData()
.
The axios call does return an array of result including the name per ids, however the <p>
tag doesnt display the names.
<script>
data(){
id: ['1', '2', '3']
},
methods:{
getData(id){
//axios call
return response.data;
}
}
</script>
Upvotes: 3
Views: 1358
Reputation: 2851
I think this is the problem:
when v-for calls your method it doesn't wait for axios call to resolve and since there is no array immediately available for it to iterate on, it can't show anything (there isn't anything to show), on the other hand when axios call does resolve there is no way for v-for to get notified and therefore update the template. so situation is something like this:
so here is what you can do:
and it should work fine, your code might be like this for example:
// template
<div v-for="data in axiosResult">
<p>{{data.name}}</p>
</div>
//script
<script>
data(){
id: ['1', '2', '3'],
axiosResult: [],
},
methods:{
getData(){
//axios call
this.id.forEach((id) => {
//axios function for each id
axiosCall(id).then((result) => {
this.axiosResult.push(result);
});
});
}
}
</script>
Upvotes: 2