Helper function in vuex actions

Suppose I have a method in

const actions = {   async fetchByQuery({     commit   }, title) {     const response = await..........code goes here   } }

And I want to use my own another function inside the method like this:

        const actions = {           async fetchByQuery({             commit           }, title) {             const response = await..........code goes here                          this.helperfunction();           }                      helperfunction(){              ......code goes here           }         }

How go I get to use helper function ?

I tried above method and got error this.helperfunction is not a function

Add Comment
1 Answer(s)

You can always import functions outside of the store, they don’t have to be part of it.

// Either  import HelperFunction from "./helperfunction.js  // OR:  const HelperFunction = () => {   console.log("Hello world!"); }  const actions = {   async fetchByQuery({     commit   }, title) {     const response = await..........code goes here          // Use the helper function without `this`     let formattedResponse = HelperFunction(response);          commit('saveState', formattedResponse);    }    } 

It’s worth noting that they won’t have direct access to modify the store, however it might be just what you need. Its difficult to say without knowing the context of your question.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.