I have a module named test1.js which is being exported default as test1.
//test1.js
function fun1(){
console.log("this is test function 1");
}
function fun2(){
console.log("this is test function 2");
}
export default 'test1';
Then another file is there named mod1.js importing test1.js as follwing-
import test1 from './test1.js';
test1.fun1();
I tried to access the function of test1.js module using the '.' which is not correct. I don't know how to access this kind of functions, even is it possible or not?
CodePudding user response:
Your syntax of exporting is wrong. You can use curly braces {}
to export:
function fun1(){
console.log("this is test function 1");
}
function fun2(){
console.log("this is test function 2");
}
export {fun1, fun2};
Similarly you can import the same way but you need to add on the file name:
import {fun1, fun2} from 'test1.js';
fun1();
fun2();
For Reference: https://javascript.info/import-export
CodePudding user response:
The export is incorrect. You need to export the functions as below:
//test1.js
function fun1(){
console.log("this is test function 1");
}
function fun2(){
console.log("this is test function 2");
}
export default { fun1, fun2 }
Since it's a default export you may import it by any name in mod1.js
import test1 from './test1.js';
test1.fun1();