关于exprot 和expert default的区别

首先咱们先知道exprot的做用是什么,exprot是用于导出模块,一般配合 improt 使用

    exprot和exprot default的区别,什么状况下用exprot,什么状况下用exprot default。

1.exprot //命名导出,用于导出多个对象code

示例代码:在b.js中引入a.js模块

a.js对象

const a=123
 const b=function(){alert(1)}

 exprot a;
 exprot b;

b.jsio

improt {a,b} from "./a.js"function

b()//弹出1变量

从上述代码咱们能够看到exprot能够在一个JS导出多个对象和变量,b.js中引入的过程须要用对应a.js中导出的变量名引入。im

exprot default //默认导出命名

a.js 文件

exprot default function(){alert(1)}co

b.js 系统

improt alertFun from "./a.js"

alertFun()//弹出1

比较与exprot导出的区别能够看到exprot default导出的对象能够自定义命名存储,无须要限制命名与a.js中的同样,可是exprot default只能默认导出一个对象,而且没法与exprot同时使用,exprot default是为全部导出对象使用系统默认命名导出,相等于只能导出一个对象

因此你要在一个文件导出多个对象就使用exprot,导出一个对象建议使用exprot default由于命名灵活性更多,不容易形成变量混乱

相关文章
相关标签/搜索