javascript 模块导入导出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 模块导入导出相关的知识,希望对你有一定的参考价值。

// Exporting individual features
export let name1, name2, …, nameN;
// also var, const

export let name1 = …, name2 = …, …, nameN; 
// also var, const

export function functionName(){...}
export class ClassName {...}

// Export list
export { name1, name2, …, nameN };

// Renaming exports
export { variable1 as name1, variable2 as name2, …, nameN };

// Exporting destructured assignments with renaming
export const { name1, name2: bar } = o;

// Default exports
export default expression;
export default function (…) { … } 
// also class, function*
                               
export default function name1(…) { … }
// also class, function*
                               
export { name1 as default, … };

// Aggregating modules
export * from …;
export { name1, name2, …, nameN } from …;
export { import1 as name1, import2 as name2, …, nameN } from …;
export { default } from …;
                               

import defaultExport from "module-name";
import { default as alias } from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { foo , bar } from "module-name/path/to/specific/un-exported/file";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
var promise = import("module-name");
                               
// Sample : module.js
function cube(x){
    return x * x * x;
}
export {cube};

// app.js
import { cube } from 'module.js';
console.log(cube(3)); // 27

以上是关于javascript 模块导入导出的主要内容,如果未能解决你的问题,请参考以下文章

javascript 模块导入导出

javascript 导出和导入模块

javascript 模块化(导入和导出文件)

LayaBox---TypeScript---模块

将 CommonJS 默认导出导入为命名导出/无法加载 ES 模块

通过导入和导出制作最少的工作 JavaScript 代码