2016-11-20 63 views
0

使用TypeScript 2.0.10,我想将我的“模型”模块在单独的文件中合并成一个单一的定义。将单个文件模块合并到单个模块中?

/lib/models 
     |-- model-a.ts 
     |-- model-b.ts 
     |-- model-c.ts 
     |-- models.ts 

我必须导入此使用相对路径,例如:import {ModelA} from "../../lib/models/model-a

我想导入这样的,而不是:`进口{} MODELA从“模型”;

我已经聚集的所有型号为models.ts

/lib/models/models.ts

declare module models { 
    export { ModelA } from "./model-a"; 
    export { ModelB } from "./model-b"; 
    export { ModelC } from "./model-c"; 
} 

我使用SystemJS所以我能够做到这一点:

paths: { 
    "*": "dist/*", 
    "models": "dist/models/models", 
    "services": "dist/services/*", 
    // ... 
} 

目前,编译将失败,因为TypeScript不知道如何找到models/*,但这个工作罚款在浏览器sinve我有SystemJS路径配置。有没有在tsconfig中设置相同类型映射的方法?

编辑重写了这一堆。

回答

0

Disclaumer:这不使用模块!(但我认为你仍然达到同样的效果)。

我还没有尝试过这与系统JS,但这应该使打字稿高兴。

型号/ index.ts

import { ModelA } from './model-a'; 
import { ModelB } from './model-c'; 

export { 
    ModelA, 
    ModelB 
}; 

某处,else.ts

import { ModelA, ModelB } from './models'; 
+1

感谢。我仍然需要使用相对路径(例如'../../ lib/models')。我真的希望简化这一点,因为我不想每次需要导入模型或服务时都要“计算”相对路径(字面意思是我的项目中的每个页面/类/等等。) –

+0

@JoshM。哦,快点!我不认为我意识到这是模块所做的,如果你知道这个问题,请用你的解决方案更新这个问题,我现在非常感兴趣! – drewwyatt

相关问题