【问题标题】:Invoking an exported function from module in TypeScript从 TypeScript 中的模块调用导出的函数
【发布时间】:2017-01-11 18:37:16
【问题描述】:

这个问题很简单。

这是模块导出函数的方式。

exports = module.exports = debug.debug = debug;

这是 ES5 模块如何使用它的。

var debug = require('debug')('http')

现在在 Typescript 中有点不同了。

import * as debug from 'debug';

这会返回一个我以前从未见过的奇怪对象,一个没有键值对的嵌套对象。

{ [Function: debug]
  coerce: [Function: coerce],
  disable: [Function: disable],
  // More properties go here
}

如何调用函数debug

【问题讨论】:

  • 您看到的第一项表明返回的对象是可调用的。您可以调用它,例如import * as debug from 'debug'; debug('http');.
  • 不,没有用。
  • ??在您的回答中,您调用 debug 的方式与我在评论中提到的完全相同。
  • 是的,完全一样。

标签: javascript typescript es6-modules


【解决方案1】:

我发现它在documentation 中挖掘

const log = debug('http');
log.log = console.log.bind(console); // binding to console

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    相关资源
    最近更新 更多