从模块引用全局变量 [英] Reference to global variable from a module
本文介绍了从模块引用全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在html标头中声明了一个全局变量,并希望从模块内的类引用它。如何防止编译错误:
I have a global variable declared in the html header and want to reference it from a class inside a module. How can I prevent compiler error:
错误TS2095:找不到符号'selfGlobal'。
<html>
<head>
<script>
var selfGlobal = this;
var globalVariable = 1;
</script>
</head>
<body>
<script src="test.js"></script>
</body>
</html>
在test.ts
module Test{
export class TestClass {
private _privateVariable:any;
constructor() {
this._privateVariable = selfGlobal.globalVariable; // compile error throws here, but the code can run
}
}
}
谢谢!
Mars
Thanks! Mars
推荐答案
你需要告诉编译器它已被声明:
You need to tell the compiler it has been declared:
declare var selfGlobal: any;
这篇关于从模块引用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文