ES6 default export function

Can I export more than one function per file? it seems that when I do this, the second function ovverides the first,

example: in a file my index.js:

export default function aFnt(){
    console.log("function a");
}
export default function bFnt(){
    console.log("function b");
}

then when i import it into my file:

import aFnt from "./index";

console.log("aFnt : ",aFnt);

the result of console.log is bFnt

what is specific here? Do I need to create a new file for each function? is it not very practical, any solution or workaround?

+4
source share
3 answers

Madox2 review works fully if you want to import named functions.

If you still want to import the default value, there is another way:

function a() {}

function b() {}

export default { a, b }

and upon import:

import myObject from './index.js';

myObject.a(); // function a
myObject.b(); // function b

Hope this helps!

+20

named export :

export function aFnt(){
    console.log("function a");
}
export function bFnt(){
    console.log("function b");
}

:

import {aFnt, bFnt} from "./index";
+9

/

export function first() {}
export function second() {}

import { first, second} from './somepath/somefile/';

, , , . - , , .

function first() {}
function second() {}
const funcs= {"first":first,"second":second}
export default funcs;

import funcs from './somepath/somefile/';
funcs.first();funs.second();

.

0

Source: https://habr.com/ru/post/1629808/


All Articles