I'm using Vue Resource to retrieve an images collection from a REST API. The request is sent in the created
hook of my Vue component.
The problem is, I'm trying to access the retrieved data in the mounted
hook, but the data isn't loaded.
I get this error in the console:
[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'forEach' of undefined"
Here is my component:
<script>
export default {
data() {
return { imgs : '' };
},
created() {
// the full url is declare in my main.js
this.imgs = this.$resource('acf/v3/pages/4');
this.imgs.query().then((response) => {
console.log('success', response);
this.imgs = response.data.acf.gallery;
}, (response) => {
console.log('erreur', response);
});
},
mounted() {
// get the ref="image" in my dom template
let imgs = this.$refs.image;
imgs.forEach((img) => {
// I do some stuff with imgs
});
}
}
</script>
If I wrap a setTimeout
around the content of mounted
, everything works fine.
So, I don't understand how I can wait for my data to load before the mounted
hook is executed. Isn't this the role of the Vue lifecycle hooks?