D
D
dicem2020-07-17 19:14:38
Vue.js
dicem, 2020-07-17 19:14:38

How to make a computed property to get the property of an object when it is not available?

Essence: there is a component that has the following properties:

computed: {
  activity() { return this.activityLoaded ? this.$store.getters.getActivityById(this.id) : null; },
  ...
  votes() { return (this.activity && this.activity.vote && this.activity.vote.count) ? this.activity.vote.count : 0 }
}

you need to pay attention to the votes property, which in fact should check whether we have something inside this.activity, this.activity.vote and this.activity.vote.count and if everything is ok then return this.activity.vote.count and if not, then return 0. And according to the logic of computed properties, it should be updated at the moment when a value appears in this.activity.vote.count , but it is not updated, in vue devtools this parameter has long been equal to 1, and still renders 0

Please help me with this issue .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-07-17
@dicem

The normal state of affairs is that if there is activity, it will always be filled with the number of existing votes.

//
  votes() {
    return this.activity ? this.activity.vote.count : null
  },
//

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question