简体   繁体   English

TypeScript:如何导入非模块化文件?

[英]TypeScript: How to import non modular file?

I'm using TypeScript.我正在使用打字稿。 Some of my files are modular using import and export (I'm using webpack to bundle them), and some are not and I use script tags to add them to the page.我的一些文件是使用importexport模块化的(我使用 webpack 来捆绑它们),而有些不是,我使用脚本标签将它们添加到页面。

I want to import a non modular TypeScript file from a modular one.我想从一个模块化的 TypeScript 文件中import一个非模块化的 TypeScript 文件。 How can I do this?我怎样才能做到这一点?

I managed to do it like this:我设法这样做:

First in your non modular file add this:首先在您的非模块化文件中添加:

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

This will export the file.这将导出文件。 Then in your modular file add this to import the file:然后在您的模块化文件中添加以下内容以导入文件:

declare var require;
let moduleToImport;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM