使用 es6 导入和调用函数

import and call a function with es6

之前:

var debug = require('debug')('http')
  , http = require('http')
  , name = 'My App';

使用es6,如何像第一行一样直接导入调用?

import debug from 'debug'();

是否可以?

import http from "debug"; // not sure if this is the desired effect

你需要两行:

import debugModule from 'debug';
const debug = debugModule('http');

导入语法是声明式导入语法,不执行任何功能。

is a no no?

正确。请记住,import 语句不仅类似于简单的 require() 语句——它还创建了 "loaded" 模块到局部变量的绑定。

import debug from 'debug'();

...更接近 behavior/semantics 到

var debug = require('debug');

...比简单

require('debug');

commonjs 风格的模块加载器的类比在某些时候显然会崩溃,但最终它是一个 "no no",因为简单明了的事实 import debug from 'debug' 不会实际上解析为您可以调用(或以其他方式引用)的任何内容。