简体   繁体   English

TypeScript requirejs加载外部模块的问题

[英]TypeScript requirejs issue with loading external modules

I am having issues with this game.js code that the game.ts file compiles to: 我遇到game.ts文件编译的game.js代码问题:

var GameObjects = require("./GameObjects")

I have set up my page index.html js imports as so: 我已经设置了我的页面index.html js imports,如下所示:

<script language="javascript" src="javascripts/require.js" type="text/javascript"> </script>
<script language="javascript" src="javascripts/jquery-1.8.2.min.js" type="text/javascript"> </script>
<script language="javascript" src="ts/GameObjects.js" type="text/javascript"> </script>
<script language="javascript" src="ts/game.js" type="text/javascript"> </script>

and this is the error in chrome: 这是chrome中的错误:

Uncaught ReferenceError: exports is not defined GameObjects.js:82
Uncaught Error: Module name "GameObjects" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded

any ideas friends? 任何想法的朋友?

I'm not sure where the idea of hand-editing the JavaScript files has come from. 我不确定手动编辑JavaScript文件的想法来自哪里。 If you compile your TypeScript with the --module amd flag, you shouldn't need to edit the JavaScript files as the import statements will be converted into require statements. 如果使用--module amd标志编译TypeScript,则不需要编辑JavaScript文件,因为import语句将转换为require语句。

The only script tag you should need on your page is this: 您页面上唯一需要的脚本标记是:

<script src="javascripts/require.js" data-main="ts/game.js"></script>

Once require.js has loaded, it will then load game.js, and whenever it comes across a require statement (which is your import statement in your TypeScript file) it will then load that script and then execute the code. 一旦require.js加载,它就会加载game.js,每当遇到require语句(这是你在TypeScript文件中的import语句)时,它就会加载该脚本然后执行代码。

You can load in jQuery and GameObjects and any other modules just by adding import statements in your TypeScript file. 只需在TypeScript文件中添加import语句,即可加载jQuery和GameObjects以及任何其他模块。

You have no data-main attribute in your require.js script tag. 您的require.js script标记中没有data-main属性。 Please read the RequireJS documentation carefully. 请仔细阅读RequireJS文档

In a nuthshell: you should be loading ts/GameObjects.js from a top-level require call, which you put in the file specified in the data-main attribute. 在nuthshell中:您应该从顶级require调用加载ts/GameObjects.js GameObjects.js,该调用放在data-main属性中指定的文件中。 eg (from the docs): 例如(来自文档):

<!DOCTYPE html>
<html>
    <head>
        <title>My Sample Project</title>
        <!-- data-main attribute tells require.js to load
             javascripts/main.js after require.js loads. -->
        <script data-main="javascripts/main" src="javascripts/require.js"></script>
    </head>
    <body>
        <h1>My Sample Project</h1>
    </body>
</html>

Then in javascripts/main.js (you can actually call it whatever you want, as long as it matches what you put in data-main ), you call require and load your modules and do whatever you want with them: 然后在javascripts/main.js (实际上你可以随意调用它,只要它与你在data-main内容相匹配),你调用require并加载你的模块并用它们做任何你想做的事情:

require(["ts/GameObjects.js", "ts/game.js"], function(GameObjects, Game) {
  ... use 'GameObjects' and 'Game' here ...
});

Remember that in ts/GameObjects.js and ts/game.js you need to wrap your code in define() calls, so they are interpreted as modules. 请记住,在ts/GameObjects.js ts/game.jsts/game.js您需要将代码包装在define()调用中,因此它们被解释为模块。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM