跨文件的Typescript全局变量 [英] Typescript Global Variable across Files

查看:397
本文介绍了跨文件的Typescript全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.ts文件中创建了一个没有模块或类的变量。它看起来就像一个普通的JavaScript文件。我希望这个变量可以在变量内部的类中的另一个.ts文件中访问。

I created a variable in a .ts file that has no module or class. It mostly looks just like a plain JavaScript file. I want this variable to accessible in another .ts file inside of a class that is inside of a variable.

所以我有:

foo.ts

var foo = "some stuff";

bar.ts

module Bar {
    export class BarClass {
        function getFoo() {
             return foo;
        }
    }
}

我不确定这个是最好的方法。我尝试过使用window.bar全局,但这似乎不起作用。我是TypeScript跳入更大代码库的新手,所以如果您需要进一步澄清任何内容,请告诉我。

I'm not sure this is the best way to do it. I've tried using the window.bar global but that doesn't seem to work. I'm new to TypeScript jumping into a larger codebase so please let me know if you need any further clarification on anything.

谢谢!

推荐答案

TypeScript文件不知道您在其他TypeScript文件中所做的任何事情,除非他们有对它们的引用。所以在 bar.ts 的顶部你应该有一行

TypeScript files don't know about anything you've done in other TypeScript files unless they have a reference to them. So at the top of bar.ts you should have a line

/// <reference path="foo.ts" />

这篇关于跨文件的Typescript全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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