TypeScript - 模块在运行时未定义 [英] TypeScript - Module is undefined at runtime

查看:60
本文介绍了TypeScript - 模块在运行时未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白我做错了什么。我在VS2012中创建了一个TypeScript项目,并在一个名为Physics的子目录中创建了一个名为Vector.ts的文件:

I dont understand what i'm doing wrong. I've create a TypeScript project in VS2012, and created a file named "Vector.ts", in a sub-directory named "Physics":

// Module
module Physics {

    // Class
    export class Vector {
        constructor(public x: number) { }
    }

}

另外,我有以下app.ts文件:

Also, I have the following app.ts file:

/// <reference path="Physics/Vector.ts" />

window.onload = () => {
    var vector = new Physics.Vector(6);
};

项目已成功编译,但是当我启动它时,我得到以下异常:

The project is successfully compiled, but when i launch it, i get the following exception:


0x800a1391 - JavaScript运行时错误:'物理'未定义

0x800a1391 - JavaScript runtime error: 'Physics' is undefined

我不明白我做错了什么......

I don't understand what am i doing wrong...

谢谢。

推荐答案

如果您使用带有模块加载程序(如Require.Js)的AMD样式模块,则需要一个import语句。请参阅Steve在此处接受的答案: https://stackoverflow.com/a/14919495/1014822 您的代码如下所示:

If you are using AMD style modules with a module loader such as Require.Js, you need an import statement. See Steve's accepted answer here: https://stackoverflow.com/a/14919495/1014822 Your code will look something like:

import Physics = module("Physics/Vector");

var vector = new Physics.Vector(6);

...你不需要这样: ///< ; reference path =Physics / Vector.ts/>

... and you won't need this: /// <reference path="Physics/Vector.ts" />

如果你没有使用模块加载器,你只需要制作确保在html页面的某处包含 Vector.js 输出文件,并确保它在您的app文件之前加载。在这种情况下,您使用 ///< reference path =Physics / Vector.ts/> 告诉TS在何处找到智能感知模块等工作。

If you are not using a module loader, you just need to make sure you include your Vector.js output file somewhere in your html page, and make sure it loads before your app file. In this case, you use /// <reference path="Physics/Vector.ts" /> to tell TS where to find your module for intellisense etc. to work.

这篇关于TypeScript - 模块在运行时未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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