Ohad Shemesh
Ohad Shemesh

Reputation: 3

Vuex: Having a problem using a getter inside a view

I have a module named 'tasks' in store which has this getter -

getTaskById: state => id => state.idToTask[id]

seems quite straightforward and basic, idToTask is just an object with ids as keys.

Now in the view (the component that displays the task) - it gets taskId as prop from router, I use mapGetters like this -

methods: {
...mapGetters('tasks', ['getTaskById'])
}

and I have a computed:

task() {
return this.getTaskById(this.taskId)
}

I really don't know what went wrong here but I get error of task being undefined (in the template) for some reason...

And I'll just say ahead that I've tried

this.$store.getters['tasks/getTaskById'](taskId)

but it does not seem to work as well...

Any help?

Upvotes: 0

Views: 96

Answers (1)

Jaybird
Jaybird

Reputation: 541

Disregard my comment, mapGetters should go in computed, not methods.

computed: {
    ...mapGetters('tasks', ['getTaskById'])
}

https://vuex.vuejs.org/guide/getters.html

https://github.com/vuejs/vuex/issues/1136

Upvotes: 1

Related Questions