【问题标题】:Is there a way to access "exports" object in TypeScript modules?有没有办法访问 TypeScript 模块中的“导出”对象?
【发布时间】:2013-05-15 01:50:14
【问题描述】:

我正在尝试使用 durandal,我需要通过 current 模块来getModuleId。我的问题是,当我使用 TypeScript 时,Typescript 代码似乎无法访问从 AMD 模块返回的底层对象:

export function checkModule(){
       var a = system.getModuleId(??);
}

编译后的 TS 会转换成这个:

function checkModule(){
     var a = system.getModule(??);
}
exports.checkModule = checkModule;

而不是?? 我需要传递在编译的TS 中定义的exports 对象。 有没有办法做到这一点,或者有一个非常简单的方法? 谢谢

【问题讨论】:

    标签: typescript durandal


    【解决方案1】:

    以下是我使用的。你是说“那里有一个出口变量”......而且有:)

    declare var exports; 
    var thisModule = exports; 
    

    【讨论】:

    • 哈!我将此标记为答案,因为它比我的更 TS-ish。谢谢@BASarat
    • 还有没有办法让exports变量被正确输入?
    • declare var exports:any
    【解决方案2】:

    有一种方法,在第一次调用模块的activate 方法时缓存this

    var _thisModule :any;
    export function activate(){
           _thisModule = this;
           ....
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-31
      • 2018-11-08
      • 2011-07-14
      • 2011-02-09
      • 2017-08-26
      • 2021-05-30
      • 2021-10-15
      • 2020-05-03
      • 1970-01-01
      相关资源
      最近更新 更多