2013-04-05 33 views
0

我導出一個簡單的功能的「log.ts」的文件裏:在同一目錄打字稿AMD模塊不返回任何

export function message(s : string) { 
    console.log(s); 
} 

這是由文件導入(「MyController.ts」):

import log = module("./log"); 
class MyController { 

    a : string = "aaa"; 

    constructor() { 
     log.message("hello world"); 
    } 
} 

在編譯時,我得到以下JS:

define(["require", "exports", "./log"], function(require, exports, __log__) { 
    var log = __log__; 

    var MyController = (function() { 
     function MyController() { 
      this.a = "aaa"; 
      log.message("hello world"); 
     } 
     return MyController; 
    })();  
}) 
//@ sourceMappingURL=MyController.js.map 

此定義函數返回myController的。因爲它沒有,這個片段裏面的回調不會對控制器參數得到任何東西:

require(["MyController"], function (controller) { 
        theRoute.controller = controller; 
        defer.resolve(); 
        $rootScope.$apply(); 
       }); 

我可以通過手動添加呼叫定義的內部回報解決這個問題,但這不是一個很好的解決方法,因爲JS正在由TS編譯器輸出。

我做錯了什麼或者這是打字稿中的錯誤?

回答

2

你應該寫:

import log = module("./log"); 
export class MyController { // <--- 'export' 
    a : string = "aaa"; 
    constructor() { 
     log.message("hello world"); 
    } 
} 

和:

require(["MyController"], function (controller) { 
    theRoute.controller = new controller.MyController(); // <-- 
    defer.resolve(); 
    $rootScope.$apply(); 
    }); 

在0.9.x版本開始,你就可以在包含.ts文件的底部寫export = MyController;使課堂成爲頂級導出對象。

+0

謝謝,這工作。 – sean 2013-04-05 23:27:04

+0

@RyanCavanaugh是TS 0.9中模塊的唯一變化嗎?這是我在規範中可以注意到的唯一一個。如果您在博客中介紹了0.9 – basarat 2013-04-28 10:44:33

+0

@RyanCavanaugh中引入的模塊更改,那麼可以申請0.8到0.9之間的內部模塊似乎相同 – basarat 2013-04-28 10:45:07