user2879626
user2879626

Reputation: 25

How to pass computed property from component somewhere else

I have working example from official site.

Example

 computed: { filteredData: function () {} )

How to get filteredData (computed property) globally? Espiccially if I want to send it somewhere else. demo.$store contains only original properties, not computed. I see the only way to achieve this is demo.$children[0].filteredData. But that is not pure. By the way, im using vuex.

Upvotes: 2

Views: 2287

Answers (1)

motanelu
motanelu

Reputation: 4025

Computed data is only available to the current component. However you can use getters in Vuex and import that as computed property where ever is needed.

In vuex, you declare this:

getters: {
  filteredData (state) {
    return state.data.filter(x => x)
  }
}

and then, where you require it, you just imported:

import { mapGetters } from 'vuex'
export default {
  // ...
  computed: {
    ...mapGetters({
      filteredData
    })
  }
  // ...
}

Upvotes: 1

Related Questions