热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

TypeScriptrequirejsissuewithloadingexternalmodules

Iamhavingissueswiththisgame.jscodethatthegame.tsfilecompilesto:我遇到game.ts文件编译的game.js代

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,如下所示:





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?

任何想法的朋友?

2 个解决方案

#1


5  

I'm not sure where the idea of hand-editing the Javascript files has come from. 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.

我不确定手动编辑Javascript文件的想法来自哪里。如果使用--module amd标志编译TypeScript,则不需要编辑Javascript文件,因为import语句将转换为require语句。

The only script tag you should need on your page is this:

您在页面上需要的唯一脚本标记是:


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以及任何其他模块。

#2


3  

You have no data-main attribute in your require.js script tag. Please read the RequireJS documentation carefully.

您的require.js脚本标记中没有data-main属性。请仔细阅读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. e.g. (from the docs):

在nuthshell中:您应该从顶级require调用加载ts / GameObjects.js,该调用放在data-main属性中指定的文件中。例如(来自文档):



    
        
        
        
    
    
        

My Sample Project

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.js中,您需要将代码包装在define()调用中,因此它们被解释为模块。

Hope that helps.

希望有所帮助。


推荐阅读
author-avatar
壹路上有祢_522
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有