2014-09-27 111 views
0

我有一個在瀏覽器環境中使用的內部模塊,我想在node.js中使用它。有沒有辦法讓它用作外部模塊。如何在內部和外部模塊之間共享代碼

int.ts

module internal_mod { 

    export class Test { 
     constructor(d:any){} 
    } 
} 

// these all will fail 
//export = internal_mod.Test; 
//export function newTest(d:any) { 
// return new internal_mod.Test(d) 
//} 

我怎樣才能在一個節點項目中使用此文件,並使用像

import t = require('../int'); 
var tt = new t(null) // or something similar to instantiate the class 

回答

0

只需使用export

module internal_mod { 

    export class Test { 
     constructor(d:any){} 
    } 
} 
export = internal_mod 

用法:

import t = require('../int').Test; 
var tt = new t(null) // or something similar to instantiate the class 
+0

謝謝basarat。它看起來像是,模塊不被識別爲內部模塊(通過// <引用..)然後,並給出錯誤'無法找到名稱internal_mod'。如果這樣做不是最好的,那麼在前端(js/angular)和後端(node) – bsr 2014-09-27 12:11:57

+1

之間共享代碼的最好方法是嗨,現在看https://github.com/basarat/demo-fullstack。 。你得到了全部覆蓋:-) – bsr 2014-09-27 12:24:38

+0

我還沒有準備好在客戶端使用require.js(可能會涉及很多對現有應用程序的更改)。所以,我想我不能按照你的例子:-( – bsr 2014-09-27 12:37:04

相關問題