codekoriko
codekoriko

Reputation: 870

How to do getter with parameter using vuex-module-decorators?

I'm trying to implement a vuex getter function (with parameter) using the Typescript/ES7 Decorators provided by vuex-module-decorators.

the normal vuex implementation would look like this:

export default new Vuex.Store({
    state: {
        students: []
    },
    getters: {
        findStudent: state => id => state.students.find(s => s.id == id)
    }
})

The vuex-module-decorators style guide says to use Typescript getter accessor

@Module
class MyModule extends VuexModule {
  students = []

  get findStudent(id) {
    return students.find(s => s.id == id)
  }
}

Therefor, of course my linter complains A 'get' accessor cannot have parameters

What would then be the best practice to implement such getter? Or should do the logic of such function into my vue component?

Upvotes: 2

Views: 993

Answers (1)

Mike Two
Mike Two

Reputation: 46173

From github

Return a function from the getter

get getUser() { return function (id: string) { /* ... */ } }

Upvotes: 2

Related Questions