TypeScript:仅包​​含语句的导入模块 [英] TypeScript: import module with only statements

查看:65
本文介绍了TypeScript:仅包​​含语句的导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个page-a.ts可以编译为page-a.js:

alert('this is from page-a');

我有一个main.ts可以编译成main.js:

import pageA = module('page-a')
alert('this is from main');

这是我的tsc命令行:

tsc --module amd page-a.ts main.ts

我正在这样使用requirejs:

<script src="require.js" data-main="main.js"></script>

加载页面时,我看不到page-a的警报消息框.在生成的脚本main.js中,与page-a无关.

I can't see the alert messagebox from page-a when loading the page. And in the generated scripts main.js, there is nothing about page-a.

我的问题是,为什么会这样?以及如何强制Typescript导入代码未明确使用的模块?

My question is, why is this happening? And how do I force typescript to import a module that is not explicitly used by the code?

推荐答案

您生成的js将不会导入未在代码中明确使用的模块. Typescript很聪明,不会为仅导入而未使用的模块生成任何js.

Your generated js will not import a module not explicitly used in code. Typescript is clever that way and will not generate any js for a module that is only imported and not used.

您需要:

  • 在第一个模块中导出变量,并且
  • 将第一个模块导入第二个模块,并且
  • 在第二个模块中使用导出的变量

所以有page-a.js之类的东西

so have page-a.js something like:

export var x = 123; // just to export something that you can use
alert('this is from page-a');

和main.ts为

import pageA = module('page-a')
var foo = pageA.x; // If you remove this line and you will not get the alert from page-a
alert('this is from main');

这篇关于TypeScript:仅包​​含语句的导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆