search

Home  >  Q&A  >  body text

Vuex Getter Hook with parameters

<p>I defined a vuex getter function with parameters, the code is as follows: </p> <pre class="brush:php;toolbar:false;">const getters = { getProjectById: (state) => (id) => { return state.projects.find(project => project.id === id) } }</pre> <p>Now, I want to use this getter function in my component, but I can't find a way to pass parameters to the getter. </p> <p>This is my getter hook computed property: </p> <pre class="brush:php;toolbar:false;">computed: { ...mapGetters(["currentUserPhoto","getProjectById"]) },</pre> <p>Is it possible to pass the Id parameter from the route to the "getProjectId" getter? If possible, what is the best approach? </p>
P粉530519234P粉530519234461 days ago574

reply all(1)I'll reply

  • P粉518799557

    P粉5187995572023-08-30 13:53:13

    Add another computed property named projectById that accepts route parameters as arguments and returns the project:

    computed: {
        ...mapGetters(["currentUserPhoto","getProjectById"]),
       projectById(){
             return this.getProjectById(this.$route.params.id)
      }
    
    },

    reply
    0
  • Cancelreply