与AMD和导出模块不匹配的匿名define()模块打字稿 [英] mismatched anonymous define() module typescript with AMD and export module

查看:146
本文介绍了与AMD和导出模块不匹配的匿名define()模块打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Typescript(1.7.5)和AMD模块创建一个应用程序.我已经添加了对r.js和requirejs.js的引用.下面是我的TS代码:

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

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

这是我的aspx页面,称为"fn()":

here is my aspx page to call "fn()":

<%@ 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未定义

"JavaScript运行时错误:匿名define()模块不匹配:函数(要求,导出)"

但是,当我删除 导出" 关键字时,上述代码可以正常工作.我是Typescript和requirejs的新手.谁能指出我做错了什么以及如何解决这个问题?

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

提前谢谢.

推荐答案

我认为您应该删除以下行:

I think you should remove the following line:

export module TestNs

类型脚本中的每个文件都是一个模块,因此您不需要该部分.

Every file in type script is a module, so you do not need that part.

编辑

然后在您的javascript脚本中删除脚本标记,以加载您的类并使用如下代码:

Then in your javascript remove 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();
})

希望这会有所帮助.

这篇关于与AMD和导出模块不匹配的匿名define()模块打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆