2016-08-31 3 views
1

В настоящее время я работаю над своим первым проектом Виджета. Пройдя официальную документацию и просмотрев несколько курсов (egghead), я подумал, что пришло время написать настоящий код - а не просто образцы.namespace-пространство имен, созданное в нескольких файлах

Я использую:

  • машинопись @ Гс (~ 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 основных пространства имен Core, компоненты и системы. Тогда, если в другом проекте этот модуль импортируется, мы могли бы сделать что-то вроде:

  • 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, где для всех namespaces компилятор говорит:

cannot re export name that is not define 

Есть ли что-то не так с тем, что я делаю?

Мне также интересно, следует ли экспортировать с использованием default в app.ts?например:

export default {Core, Components, Systems} 

Будет ли это помогать при импорте моего модуля?

Thanks, Ред.

ответ

0

Не уверен, что мой вопрос был достаточно ясным. Я нашел решение, хотя и хотел бы ваше мнение.

Итак, я удалил все пространства имен из своих файлов. Это означает, что теперь все мои файлы являются модулями.

Тогда в корне моего решения создать три файла:

  • _core.ts
export * from "./core/core" 
  • _components.ts
export * from "./core/components" 
export * from "./input/keyboard" 
  • _systems.ts
export * from "./core/systems" 
export * from "./input/input.controller" 

Теперь в моем app.ts я могу просто импортировать эти новые файлы, как это.

import * as Core from "./_core" 
import * as Components from "./_components.ts" 
import * as Systems from "._/systems.ts" 

export = {Core, Components, Systems} 

Что вы думаете? Любая потенциальная проблема?

Thanks, Ред.