2013-07-20 113 views
6

我有一个全局变量在html头文件中声明,并希望从模块中的类引用它。如何防止编译器错误:从模块引用全局变量

错误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 

     } 
    } 
} 

谢谢! 火星

+1

参见http://stackoverflow.com/questions/13252225/call-a-global-variable-inside-typescript-module – koppor

回答

9

你需要告诉它已申报编译:

declare var selfGlobal: any; 
+1

不妨是'声明var selfGlobal:Window;':) – basarat

+1

谢谢你的工作。我得到了一个针对globalVariable的编译器错误,但是在我声明globalVariable之后它也消失了。 –