export default 和 export 区别:
1.export与export default均可用于导出常量、函数、文件、模块等
2.你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使用
3.在一个文件或模块中,export、import可以有多个,export default仅有一个
4.通过export方式导出,在导入时要加{ },export default则不需要
这样来说其实很多时候export与export default可以实现同样的目的,只是用法有些区别。注意第四条,通过export方式导出,在导入时要加{ },export default则不需要。使用export default命令,为模块指定默认输出,这样就不需要知道所要加载模块的变量名。
var name="李四"; export { name } //import { name } from "/.a.js" 可以写成: var name="李四"; export default name //import name from "/.a.js" 这里name不需要大括号
再看第3条,在一个文件或模块中,export、import可以有多个,export default仅有一个,也就是说如下代码:
var name1="李四"; var name2="张三"; export { name1 ,name2 }
也可以写成如下,也是可以的,import跟他类似。
var name1="李四"; var name2="张三"; export name1; export name2;