如何使用Typescript 1.5加载外部AMD模块? [英] How to load external AMD module using Typescript 1.5?

查看:183
本文介绍了如何使用Typescript 1.5加载外部AMD模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2015,并且已经创建了TypeScript HTML应用程序. 我还制作了最简单的外部模块,像这样.

I'm using VS2015 and I've created a TypeScript HTML application. I also made the simplest possible external module, like this.

export class MyClass {
    public Test(): string {
        return "Erik";
    };

    constructor() {
    };
};

然后在我的app.ts文件中,我写了以下内容.

Then in my app.ts file i wrote the following.

import E = require('./Erik');

window.onload = () => {
    let c: E.MyClass = new E.MyClass();
    alert(c.Test());
};

在Chrome中使用F12调试,我可以看到此错误.

Using F12 dubugging in Chrome I can see this error.

未捕获的ReferenceError:未定义

Uncaught ReferenceError: define is not defined

我想念什么?我确保在项目设置中选择了AMD模块类型,并在编译时创建了Erik.js文件.

What am I missing? I have made sure that AMD moduletype is selected in the project settings and a Erik.js file is created when compiling.

推荐答案

您仍然需要使用与AMD模块兼容的模块加载器库.

You still need to use a module loader library that works with AMD modules.

例如,您可以在应用程序中包含 require.js ,然后按照以下步骤进行操作:

For example, you could include require.js in your application, then do something along these lines:

<script data-main="scripts/app" src="scripts/require.min.js"></script>

data-mainapp.js的路径.

这篇关于如何使用Typescript 1.5加载外部AMD模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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