简体   繁体   English

打字稿中的参考和模块

[英]References and Modules in Typescript

I am building a project in Typescript. 我正在使用Typescript构建一个项目。 I have a file xyz.ts which has a Module defined as X Where I have a class a. 我有一个文件xyz.ts,其模块定义为X我在哪里有一个类a。 Similarly I have another class abc.ts also under the Module x, having class b. 同样地,我在模块x下有另一个类abc.ts,有类b。

//file xyz.ts
module X {
'use strict';
export class a{
}

//File abc.ts
module X {
'use strict';

export var appData: any;
export class b{
}

If I try to access appData from xyz.ts, it doesn't. 如果我尝试从xyz.ts访问appData,它不会。 However in compiled JS(gained by commenting the code which is trying to access the file, and then manually uncomment the line in generated js) it works fine. 但是在编译的JS中(通过注释试图访问文件的代码获得,然后手动取消注释生成的js中的行)它可以正常工作。

What would be the appropriate route to access and build in this scenario ? 在这种情况下访问和构建的适当途径是什么? Also, Would I be able to access files having same module, but separated by projects ? 此外,我能够访问具有相同模块但由项目分隔的文件吗? (two different Typescript project, both having same Module) [Though that would be illogical] (两个不同的Typescript项目,都有相同的模块)[虽然这是不合逻辑的]

In order to use appData from xyz.ts, you will need to import it: 要从xyz.ts使用appData,您需要导入它:

// xyz.ts
import abc = require("./abc")
var myAppData = abc.X.appData;

Hope this helps. 希望这可以帮助。

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

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