Router custom functions in Sage 9 w/ Yarn (etc.)

Hi there,

This is probably a very simple ‘problem’, but I just can’t find the solution documented anywhere.

I have a custom route file for my CPT, called single-module.js. It is correctly loaded, and I can test this by running some code in the init function for the route.

However, I want to be able to call another, custom function inside this route, and I’m not sure how. I have this (pseudo) code:

export default {
  init() {
		/* Get some data into an array, and call the doStuff function on each item */
		doStuff(x);
  },
  doStuff(x){
	 //Whatever
	}
};

And whether I try doStuff(x); or this.doStuff(x); (or any variant on this that I can think of), I get this.doStuff is not a function.

Does anybody know the correct way to call the doStuff function from within this single-module.js file?

If the above isn’t the way to go about it (functions not allowed in custom routes?), can anybody offer any pointers on how to call a function that will only ever be used in the single-module.js file?

Thanks a lot!

EDIT:

Solved!

The above code becomes:

import doStuff from '../util/do-stuff';
export default {
  init() {
		/* Get some data into an array, and call the doStuff function on each item */
		doStuff(x);
  }
};

And do-stuff.js is simply:

export default theParam => {
  //Whatever
};

:slight_smile:

Thanks to @bokorir

2 Likes