我希望在ecma脚本6中将不同的模块导入到我的模块中。
import rest from 'rest';
export function client() {
// some logic
}如果我将导入语句更改为经典语句:
var rest = require('rest');一切都很好。有什么想法吗?
发布于 2016-01-02 20:28:22
这是我的问题的答案,但是如果您想知道如何导入其他文件,请参考user @mido给出的答案,或者例如查看以下页面:http://www.2ality.com/2014/09/es6-modules-final.html
所以@Felix的评论让我找到了正确的答案。正如Felix所建议的,rest模块没有默认的导出函数,因此应该像这样导入它:
import * as rest from 'rest';因此,它取决于一个模块,它是如何编写的。例如,rest中包含的"mime“拦截器模块可以包括:
import mime from 'rest/interceptor/mime';发布于 2015-12-31 03:06:10
我不是专家,但import在许多方面与require相似,但关键的区别是:
import导入选择性项(猜这与python很接近),但是使用require,您只能导出一个模块作为命名空间,其他的都是它的子模块。require更多的是node.js特性(尽管您可以使用browserify将其引入浏览器),但是import现在是ES6的原生特性,即支持ES6的浏览器,import可以工作。从lukehoban's es6features的例子可以重新执行我的第一点:
// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
alert("2π = " + math.sum(math.pi, math.pi));
// otherApp.js
import {sum, pi} from "lib/math";
alert("2π = " + sum(pi, pi));
//Some additional features include export default and export *:
// lib/mathplusplus.js
export * from "lib/math";
export var e = 2.71828182846;
export default function(x) {
return Math.log(x);
}
// app.js
import ln, {pi, e} from "lib/mathplusplus";
alert("2π = " + ln(e)*pi*2);https://stackoverflow.com/questions/34540318
复制相似问题