node require()与es6 import:为什么这个例子不起作用 [英] node require() versus es6 import: why doesn't this example work

查看:81
本文介绍了node require()与es6 import:为什么这个例子不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是不是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆