【问题标题】:How do you use typescript internal modules with AMD modules您如何将 typescript 内部模块与 AMD 模块一起使用
【发布时间】:2015-12-27 08:29:57
【问题描述】:

我不确定我的打字稿结构是否不正确,所以可能在这里问错了问题。

我在同一个文件夹的不同文件中有 2 个相关的类 1 个接口。

我将它们包装在一个模块中,因为这感觉就像我应该从 C# 中做的那样。

这都是 angularjs,所以它是自己的 DI,这可能很重要,但可能并不重要。

文件 1:

export module Module1{
    export interface IService{
    }
}

文件2:

export module Module1{
    export class Implementation implements IInterface{
    ...
    }
}

文件 3 是使用角度注入的 IInterface 实例的角度代码。如果我使用 require("./File2") 导入 File2 它可以工作,但我宁愿导入整个 Module1,如下所示,所以我不必单独要求每个类(因为这显然是一个简化的案例)。

import authModule = require('Module1');

var assetStreamApp = angular.module("[])
    .run(['IInterface'], (instance: IInterface) => {
        instance.doSomething();
    });

这可能吗?

我不想单独导入每个文件,然后为每个“模块”选择不同的别名来命名类型,因为我觉得我应该可以这样做一次。

编辑:在阅读了更多内容之后,我想我已经了解了一些术语。我想在项目中使用 typescript 内部模块,但也使用 AMD 模块作为拆分点,这样我就可以使用 webpack 的代码拆分。

【问题讨论】:

    标签: typescript amd webpack


    【解决方案1】:

    理想情况下,您应该只使用外部模块,而不是将内部模块与外部模块混合使用。

    这已在herehere 进行了详细讨论。

    我建议做... IService.ts:

    interface IService {
    }
    
    export = IService;
    

    实施.ts:

    import IInterface = require("./IInterface");
    
    class Implementation implements IInterface{
    ...
    }
    
    export = Implementation;
    

    然后将它们适当地导入到您的文件中:

    import IService = require("./IService");
    import Implementation = require("./Implementation");
    
    // use IService and Implementation here
    

    将多个模块组合成一个模块

    话虽如此,如果你真的想这样做,你可以使用上面的IService.tsImplementation.ts,然后创建一个名为Module1.ts 的文件,在其中导入然后导出模块,如下所示:

    export import IService = require("./IService");
    export import Implementation = require("./Implementation");
    

    然后在你的代码中你可以像这样使用它:

    import Module1 = require("./Module1");
    
    // use Module1.IService or Module1.Implementation here
    

    将多个模块与 ES6 模块组合

    顺便提一下,如果你使用 ES6 模块,这样做会很方便...

    IService.ts:

    interface IService {
    }
    
    export default IService;
    

    实施.ts:

    import IInterface from "./IInterface";
    
    export default class Implementation implements IInterface {
    ...
    }
    

    模块1.ts:

    // re-export the modules
    export {default as IService} from "./IService";
    export {default as Implementation} from "./Implementation";
    

    然后,当您使用此模块时,您可以轻松访问您想要的内容。以下是一些示例导入语句:

    // import only IService
    import {IService} from "./Module1";
    // import IService and Implementation
    import {IService, Implementation} from "./Module1";
    // or implement everything on a Module1 object
    import * as Module1 from "./Module1";
    

    【讨论】:

    • 谢谢,我会看看那些链接。那和export interface IService有什么区别?
    • @BenCr export interface IService 会将其导出为模块的属性,而不是模块本身。所以通过使用export = IServiceimport IService,那么IService就是实际的接口。
    • 啊,太好了,这简化了别名,所以我不必import module = require('');,然后在任何地方都使用module.IService。太好了,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多