【发布时间】:2016-08-31 23:57:16
【问题描述】:
我目前正在处理我的第一个 Typescript 项目。在浏览了官方文档并观看了一些课程(蛋头)之后,我认为是时候编写真正的代码了——而不仅仅是示例。
我正在使用:
- typescript@rc (~2.0.2)
- Visual Studio 代码 (~1.4.0)
- Windows 10
所以我正在开发一个节点模块。我的问题是关于代码结构的。
这是我的项目的样子:
src/
|____ core/
| |____ core.ts
| |____ components.ts
| |____ systems.ts
|____ input/
| |____ input.system.ts
| |____ keyboard.ts
|____ app.ts
以下是每个文件的示例:
- core.ts
/// <reference path="../core/components.ts" />
namespace Core {
export interface IEntity {
x: number
y: number
components: [Components.IComponent]
update(dt: number): void
}
export class Entity implements IEntity {
x: number
y: number
components: [Components.IComponent]
update(dt: number): void{
// do something with the coordinates
}
}
}
- components.ts
namespace Components{
export interface IComponent {
update(dt: number): void
// some other stuff here...
}
}
- systems.ts
namespace Systems{
export interface ISystem{
update(dt: number): void
// some other stuff here...
}
}
- input.system.ts
/// <reference path="../core/systems.ts" />
namespace Systems{
export class InputSystem implements ISystem{
update(dt: number): void{
// do something here
}
// some other stuff here...
}
}
- keyboard.ts
/// <reference path="../core/components.ts" />
namespace Components{
export class Keyboard implements IComponent{
update(dt: number): void{
// do something here - Catch key up / down
}
// some other stuff here...
}
}
- app.ts
/// <reference path="./core/core.ts" />
/// <reference path="./core/components.ts" />
/// <reference path="./core/systems.ts" />
/// <reference path="./input/input.system.ts" />
/// <reference path="./input/keyboard.ts" />
export = {Core, Components, Systems}
我在这里要做的是拥有 3 个主要的命名空间核心、组件和系统。然后如果在另一个项目中导入了这个模块,我们可以这样做:
- other.module.ts
// load module from node
import * as mymodule from "mymodule"
module A {
class A extends mymodule.Core.Entity{
constructor() {
this.components.push(new mymodule.Components.Keyboard());
}
}
export function main(): void{
var systems: [mymodule.Systems.ISystem];
systems.push(new mymodule.Systems.InputSystem());
// other systems could be pushed Here
for(var system in systems){
system.update(0.5);
}
}
}
我得到的错误是在 app.ts 中,所有命名空间的编译器都说:
cannot re export name that is not define
我在做什么有问题吗?
我还想知道是否应该在 app.ts 中使用默认值导出?喜欢:
export default {Core, Components, Systems}
这对导入我的模块有帮助吗?
谢谢, 编。
【问题讨论】:
标签: node.js typescript