ES6 export default function

前端 未结 3 766
臣服心动
臣服心动 2021-02-04 03:46

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

example : in my index.js file

3条回答
  •  深忆病人
    2021-02-04 04:34

    there are couple of ways to export and import objects/functions

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

    in other file

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

    if you want to use default, in general if there is only one export in a file it should be a default export. but if you for some reasons want two functions as default then you have to club them as a object and export that object as default

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

    in other file

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

    this should be it.

提交回复
热议问题