Inconsistent anonymous define () typescript module with AMD and export module

I am trying to create an application using Typescript (1.7.5) and an AMD module. I've added a link to r.js and requirejs.js. below is my TS code:

export module TestNs {
    export class TestClass {
        public fn(): void{
            debugger;
        }
    }
}

      

here is my aspx page for the "fn ()" call:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TypeScriptPage.aspx.cs" Inherits="WebApplication1.TypeScriptPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="div1">
        </div>
        <script src="Scripts/r.js"></script>
        <script src="Scripts/require.js"></script>
        <script src="Scripts/TypeScriptBegin.js"></script>
        <script type="text/javascript">

            var testObj = new TestNs.TestClass();
            testObj.fn();

        </script>
    </form>
</body>
</html>

      

but i am getting below errors:

TestNS undefined

"JavaScript runtime error: Inconsistent anonymous define (): function (require, exports)"

however, when I remove the "export" , the code above works fine. I am new to Typescript and requirejs. Can anyone point out what I am doing wrong and how to fix it?

early.

0


source to share


2 answers


I think you should remove the following line:

export module TestNs

      

Every file in script type is a module, so you don't need that part.

EDIT



Then, in your javascript, remove the script tag to load your class and use something like this:

var moduleName = 'Scripts/TypeScriptBegin';
require([moduleName], function(TestNs){
            var testObj = new TestNs.TestClass();
            testObj.fn();
})

      

Hope this helps.

+1


source


Thanks to Amid, I found the answer (need any body anyway):

TypeScriptBegin.ts

export module TestNs {
    export class TestClass {
        public fn(): void {
            //var v = new TSTest.TSTest.TestClass();
            debugger;
            //v.fn();
        }
    }
}

      



call from aspx:

<script src="Scripts/r.js"></script>
        <script src="Scripts/require.js"></script>
        <script type="text/javascript">

            var moduleName = 'Scripts/TypeScriptBegin';
            require([moduleName], function (TestMod) {
                var testObj = new TestMod.TestNs.TestClass();
                testObj.call();
            });

        </script>

      

0


source







All Articles