ES6导出默认功能 [英] ES6 export default function
本文介绍了ES6导出默认功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以为每个文件导出多个函数吗?
好像当我这样做时,第二个函数会抛出第一个函数,
can I export more than one function per file ? it seems like when I do that , the second function ovverides the first one ,
例如:
in my index .js
文件:
export default function aFnt(){
console.log("function a");
}
export default function bFnt(){
console.log("function b");
}
然后当我在我的文件中导入它时:
then when I import it in my file :
import aFnt from "./index";
console.log("aFnt : ",aFnt);
console.log的结果是bFnt
the result of the console.log is bFnt
这究竟是什么情况?我是否必须为每个功能创建一个新文件?这不是很实用,任何解决方案或解决方法?
what exactly is the case here ? do I have to create a new file per function ? that is not very practical , any solution or workaround ?
推荐答案
madox2 如果要导入命名函数,则答案完全有效。
madox2's answer totally works if you want to import named functions.
如果您仍想导入默认值,还有另一种技巧:
If you still want to import the default, there's another technique:
function a() {}
function b() {}
export default { a, b }
以及导入时:
import myObject from './index.js';
myObject.a(); // function a
myObject.b(); // function b
我希望这会有所帮助!
这篇关于ES6导出默认功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文