繁体   English   中英

如何使用新的es6模块语法表达此Node导出

[英]How to express this Node export in the new es6 module syntax

如何用新的es6模块语法对这种(重新)导出进行编码:

math.js

module.exports = {
   PI: 3.14
};

module.js

const myMath = require('./math.js');

function myFunc() {}

module.exports = {
   PI: myMath.PI, // now is this done in es6?
   myFunc: myFunc
};

有几种方法,最简单的版本

math.js

export default {
  PI: 3.14
};

module.js

import myMath from './math.js';

function myFunc() {}

export default {
  PI: myMath.PI,
  myFunc: myFunc
};

一种不同的处理方式,在模块上有多个导出:

math.js

export const PI = 3.14;
export default {
  PI: PI
};

module.js

import PI from './math.js';

function myFunc() {}

export default {
  PI: PI,
  myFunc: myFunc
};

在这种情况下,我们仅从数学模块中导入PI

注意:您必须使用编译器才能正常工作,节点尚不支持ES6模块。

另一种方法是使用ES6提供的重新导出功能:

# math.js
export const PI = 3.14;

# module.js
export {PI} from './mymath.js';

export function myFunc() { /* TODO */ }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM