Mar*_*Zhu 6 javascript typescript
我有一个在html标头中声明的全局变量,并希望从模块内的类引用它.如何防止编译器错误:
错误TS2095:找不到符号'selfGlobal'.
<html>
<head>
<script>
var selfGlobal = this;
var globalVariable = 1;
</script>
</head>
<body>
<script src="test.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在test.ts中
module Test{
export class TestClass {
private _privateVariable:any;
constructor() {
this._privateVariable = selfGlobal.globalVariable; // compile error throws here, but the code can run
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!火星
mak*_*mak 10
您需要告诉编译器它已被声明:
declare var selfGlobal: any;
Run Code Online (Sandbox Code Playgroud)