2017-01-24 30 views
1

我正在使用TypeScript。我的一些文件模塊化使用importexport(我使用webpack捆綁它們),有些則不是,我使用腳本標記將它們添加到頁面。TypeScript:如何導入非模塊化文件?

我想import從模塊化的一個非模塊化的TypeScript文件。 我該怎麼做?

回答

1

我設法做到這一點是這樣的:

首先在你的非模塊化的文件中添加此:

if (typeof module !== 'undefined' && module['exports']) { 
    module['exports'] = varToExport; 
} 

這將導出的文件。 然後在你的模塊化文件中加入這個來導入文件:

declare var require; 
let moduleToImport; 

if (typeof module !== 'undefined' && module['exports']) { 
    var moduleToImport = require('./...file address...'); 
}