export命名导出与默认导出


使用命名导出

// module "my-module.js"
function cube(x) {
  return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
export { cube,foo };
 
 
在其它脚本 (比如import)

import { cube, foo } from 'my-module.js';
console.log(cube(3)); // 27
console.log(foo);    // 4.555806215962888

使用默认导出

// module "my-module.js"
export default function cube(x) {
  return x * x * x;
}
在另一个脚本中,可以直接导入默认导出
// module "my-module.js"
import cube from 'my-module';
console.log(cube(3)); // 27​​​​​


猜你喜欢

转载自blog.csdn.net/chi1130/article/details/80691507