You could add an index.js
file in the same directory as all the Javascript files that you want to import. Then, import all the functions into this one index.js
file. This way, you won't have to list out each and every one of the functions (or their files) everywhere you use them.
export default myFunc1 // first.js
export default myFunc2 // second.js
export default myFunc3 // third.js
...
index.js file would be
import myFunc1 from "./scripts/first.js"
import myFunc2 from "./scripts/second.js"
import myFunc3 from "./scripts/third.js"
...
export {
myFunc1,
myFunc2,
myFunc3,
...
};
Suppose these function files and the index.js file are located in a directory named functions
, and you want to access them from a file that is one level higher you would do:
import "./functions"; // Or "functions"
Now, keep in mind that this solution is only desirable if you are using these functions in more than one place. If you are not, you can use your original method.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…