admin管理员组

文章数量:1295112

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 piler error:

error TS2095: Could not find symbol 'selfGlobal'.

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

In test.ts

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

        }
    }
}

Thanks! Mars

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 piler error:

error TS2095: Could not find symbol 'selfGlobal'.

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

In test.ts

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

        }
    }
}

Thanks! Mars

Share Improve this question edited Jul 20, 2013 at 17:01 Mars Zhu asked Jul 20, 2013 at 16:55 Mars ZhuMars Zhu 2962 silver badges14 bronze badges 1
  • 1 See also stackoverflow./questions/13252225/… – koppor Commented May 14, 2016 at 22:24
Add a ment  | 

1 Answer 1

Reset to default 10

You need to tell the piler it has been declared:

declare var selfGlobal: any;

本文标签: javascriptReference to global variable from a moduleStack Overflow