on a component I'm receiving blog object through props
props: {
blog: {
type: Object,
default: null,
},
}
here I declare comments in data
data: () => ({
comments: [],
// rest needed variables
}),
and on the created method of that component I'm doing this:
created() {
if (this.blog && this.blog.comments) {
this.comments = [];
this.$nextTick().then(() => {
this.comments = this.$srv.myMethod(this.blog.comments);
console.log('this.comments);
this.$root.$emit('comments', this.blog, this.comments);
});
} else {
// rest of code
}
},
the problem here is that this.blog.comments isn't updated.
I expect to get changes whenever a change occur. any insights please ?
You are using created hook that is only executed 1 time when you create the component.
To check your prop changes you can use a watcher:
watch: {
blog: {
handler(newValue) {
// Your stuff here newValue contains the blog object updated
},
deep: true
}
}
But if you only need blog.comments maybe is better to use your prop like blogComments as array and watch only this prop instead the full object.