[Vuejs]-Call function in store.js from other js file in Vue

1πŸ‘

βœ…

If you use a module loader like webpack, you need to import the function inside your store.js

const {functionA} = require('./serviceMixin');

Then export functionA

function functionA(){

}

module.exports = {
    functionA
}

3πŸ‘

Let the first file be First.js and second file be Second.js and both of them be in same folder. The below code is of File First.js

export default function FunctionOne(){
    console.log("from file one")
}

Second.js

import FunctionOne from './First.js'
FunctionOne()

1πŸ‘

In your serviceMixin.js file you should define the function correctly by adding the function keyword and export it like :

 export function functionA(){
      console.log("Hello");
            }

then in your store file import it in the top and call it in the action :

import {functionA} from './serviceMixin'
... 
mutations: {
      autoUpdateDb(state, payload) {
       functionA();
    },

Leave a comment