node require()与es6 import:为什么这个例子不起作用 [英] node require() versus es6 import: why doesn't this example work
问题描述
是不是ES6替换节点require()?我有以下代码:
Isn't ES6 replacing node require()? I've got the following code:
var sprintf = require(sprintf-js)。sprintf;
这与宣传的一样。我可以使用sprintf()。我想使用ES6导入语句完成相同的操作:
This works as advertised. I can just use sprintf(). I'd like to accomplish the same using ES6 import statements:
从'sprintf-js'导入sprintf;
这不起作用。为什么不?我该如何解决?如果您可以解释sprintf-js中的导出如何工作,则可以获得奖励。
This does not work. Why not? How can I fix it? Bonus points if you can explain how the exports work inside sprintf-js.
推荐答案
您可以通过多种方式访问模块导出。 请参阅MDN文章。
You can access module exports in a number of ways. See the MDN article.
import defaultMember from "module-name";
import * as name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import defaultMember, { member [ , [...] ] } from "module-name";
import defaultMember, * as name from "module-name";
import "module-name";
在这种情况下,如果您已分配的导出,则语法将有效sprintf
为默认
;如果您已将 sprintf
分配给默认
对象。
In this case, your syntax would work if you had assigned the export of sprintf
to be default
; if you had assigned sprintf
to the default
object.
假设它不是,正确的语法将在大括号内包含对导出方法的引用。
Assuming its not, a correct syntax would incorporate references to the exported method within curly-braces.
import { sprintf } from 'sprintf-js';
这篇关于node require()与es6 import:为什么这个例子不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!