How to use vuex namespaced getters with arguments?
Asked Answered
N

3

15

I have issue when Im using vuex.

I have getters in namespaced module and I cant figurę out how to get the data with Ii when Im passing some arguments.

this.$store.getters.feeders.getFeedersById(id)

And in maper.

...mapGetters({
   feeders: ['feeders/getFeedersById'](this.id)

Getting error like this getter is not a function. What else shoudl I do?

Neglect answered 23/1, 2018 at 11:13 Comment(4)
Have you seen this question on the official GitHub page maybe: github.com/vuejs/vuex/issues/688Sanatorium
Still not working for the edited functionNeglect
Works now :D Thanks! :PNeglect
So, is the final edit the one working with namespaces? Maybe someone would come along with the same issue and can be able to see where s/he should fix ;)Sanatorium
N
24
feedersById(state) {
    return rowId => {
        if (state.feedersArray.hasOwnProperty(rowId)) {
            return state.feedersArray[rowId].map(id => state.feeders[id]);
        }

    }
},

feedersId() {
  if (this.rowData) {
   return this.$store.getters['feeders/feedersById'](this.rowData.ac_id);
  }
}

Okey I had some mistake there and now it works properly. Thanks!! :)

Neglect answered 22/5, 2018 at 6:37 Comment(0)
A
3

You can also declare a getter function like this:

feedersById: (state) => rowID => {
  if (state.feedersArray.hasOwnProperty(rowId)) {
    return state.feedersArray[rowId].map(id => state.feeders[id]);
  }
}
Ameliorate answered 16/7, 2018 at 17:15 Comment(0)
T
2

The easier way is to use name spaced getters:

computed(){
    ...mapGetters('feeders', ['getFeedersById'])
}
Tatia answered 31/10, 2020 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.