简体   繁体   English

用requireJS加载一些打字稿模块

[英]loading some typescript modules with requireJS

I have a problem with the connection of some typescript classes using RequireJs. 使用RequireJs连接某些打字稿类时遇到问题。 In .ts files I use the modules syntax and AMD modules. 在.ts文件中,我使用模块语法和AMD模块。 And my main file is GameLoader.ts which returns a instance of the game. 我的主文件是GameLoader.ts,它返回游戏实例。 There for example I'm creaiting World instance: 例如,我在创建世界实例:

import World = require('Resources/Scripts/Game/Entities/Game/World');
...
var world = new World(json.world.level, json.world.size, json.world.passMap);

and it works fine - World really required function 它工作正常-世界真正需要的功能

But at the same the same file (GameLoader.ts): 但是在同一文件(GameLoader.ts)上:

import Camera = require('Resources/Scripts/Game/Presentation/Camera/Camera');
...
var camera = new Camera();

Camera is empty object (instead of the function) Uncaught TypeError: Camera is not a function The file is loaded, but the debugger showed that its contents are not used - breakpoint within the function define doesn't work. 照相机是空对象(而不是函数) 未捕获的TypeError:照相机不是函数已加载文件,但调试器显示未使用其内容-函数定义中的断点不起作用。 But in World breakpoint is triggered prior to loading the main program. 但是在World中,在加载主程序之前会触发断点。

Finding fault I tried to remove all things from Camera.ts. 发现故障我试图从Camera.ts中删除所有内容。 After removal of I called empty Camera in the same manner from another file (temp.ts): 删除后,我从另一个文件(temp.ts)以相同的方式调用了空的Camera:

import c = require('Resources/Scripts/Game/Presentation/Camera/Camera');
var cam = new c();
console.log(cam);

and it worked. 而且有效。

Ts-generated code by compiler (for Camera.js) in both cases looks the same. 在两种情况下,由编译器(用于Camera.js)生成的Ts代码看起来都是相同的。 To call compiled files used the same way - by call RequireJs API. 调用编译文件的方法相同-通过调用RequireJs API。

Perhaps the problem is cyclical references that point to a file GameLoader? 也许问题在于指向文件GameLoader的循环引用? But my application built from classes that game (which instance exported from GameLoader) is used only in theirs instances. 但是我的游戏类(从GameLoader导出的实例)的类构建的应用程序仅在其实例中使用。

Sorry for my bad English. 对不起,我的英语不好。

Perhaps the problem is cyclical references that point to a file GameLoader? 也许问题在于指向文件GameLoader的循环引用?

Probably. 大概。 Give the dependency view in atom-typescript a go https://github.com/TypeStrong/atom-typescript#dependency-view 赋予atom-typescript中的依赖关系视图https://github.com/TypeStrong/atom-typescript#dependency-view

I shot in my leg with RequireJs. 我用RequireJs开枪。 Problem there that I tried to return instance from shared module. 我尝试从共享模块返回实例的问题。 And every dependency tried to create new. 每个依赖都试图创建新的依赖。 So at first time - in my case - some modules missed on load other deps stage. 因此,在我自己的情况下,第一次,某些模块在其他deps阶段加载时丢失。

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

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