如何在新的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
};
答案 0 :(得分:3)
有几种方法可以做到,最简单的版本
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模块。
答案 1 :(得分:3)
另一种方法是使用ES6提供的reexport功能:
# math.js
export const PI = 3.14;
# module.js
export {PI} from './mymath.js';
export function myFunc() { /* TODO */ }