M
M
Maria_Gavrilova2019-08-06 01:23:19
JavaScript
Maria_Gavrilova, 2019-08-06 01:23:19

Lost __ob__ observer on array from vuex state?

Hello. I am writing an app in vue. I have arrays of objects in my vuex state: posts and comments. Objects from the comments array have a post_id field.
In the component, I define the computed property like this:

computed: {
    comments() {
            return this.$store.state.comments.filter(comment => comment.post_id === this.postId);
      }
}

If objects in the comments array were initially declared in the state itself, then in this case, if we print this computed property to the console, we will see - [{}, __ob__: Observer] , they are under observation.
But if I push objects into the comments array through the newComment mutation like this:
newComment(state, payload) {
    let comment = {...}
    state.comments.push(comment);
}

In this case, if we print this computed property to the console, we will see something like this: [{…}] , i.e. __ob__ observer is ABSENT)
Question - how to push objects to the state so that they do not lose __ob__ observer?
ps I invented these entities, my task is actually more difficult, I simplified it, leaving the essence. Tell me please. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2019-08-06
@Maria_Gavrilova

return this.$store.state.comments.filter(comment => comment.post_id === this.postId);

So it doesn't work in this case? It seemed like it should.
If not, then file the filtering in the state itself, an example at the same link. Although there is no reason for the computed option not to work, unless you simplify this very reason for the question :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question