D
D
Dmitry Volkhontsev2018-08-08 09:38:04
Vue.js
Dmitry Volkhontsev, 2018-08-08 09:38:04

Is it possible for computed properties to work when getting data for them from vuex?

Good day.
There is something like this code:

computed: {
    myData() {
      let data = [] || this.$store.getters.getMyData();
      return data;
    },
    isValid(){
        return this.myData.list.some( el => el.id === this.id )
    }
  },

But the error "Error in render: "TypeError: Cannot read property 'some' of undefined"" is thrown. I understand that vuex does not have time to work out before the calculation of the isValid property. In this regard, the question is, is such an approach generally feasible to implement? Or is it easier to put the necessary properties in data and change them in myData after the getter is executed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#FFFFFF, 2018-08-08
@DarkDD

As an option:

isValid(){
  let data = [];
  if(this.myData && this.myData.list) data = this.myData.list.some( el => el.id === this.id );
  return data
}

Until the data comes from the server, the property will return empty.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question