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

890 Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

From github

Return a function from the getter

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