gpt4 book ai didi

javascript - 引用模块中的全局变量

转载 作者:数据小太阳 更新时间:2023-10-29 05:08:06 26 4
gpt4 key购买 nike

我在 html header 中声明了一个全局变量,并想从模块内的类中引用它。如何防止编译器错误:

错误 TS2095:找不到符号“selfGlobal”。

<html>
<head>
<script>
var selfGlobal = this;
var globalVariable = 1;
</script>
</head>
<body>
<script src="test.js"></script>
</body>
</html>

在测试中

module Test{
export class TestClass {
private _privateVariable:any;
constructor() {
this._privateVariable = selfGlobal.globalVariable; // compile error throws here, but the code can run

}
}
}

谢谢!火星

最佳答案

你需要告诉编译器它已经被声明了:

declare var selfGlobal: any;

关于javascript - 引用模块中的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17764468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com