构成Type Script项目的多个文件 [英] Multiple files making up Type Script project

查看:83
本文介绍了构成Type Script项目的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在使用TypeScript,一切都很好,我真的很喜欢..让JavaScript再次可行! :)

Recently I have been working with TypeScript and everything has been fine and I really, really like it.. Makes JavaScript workable again! :)

我们试图遵循与任何.net项目背后相同的想法,例如每个类/接口的单个​​文件

we have tried to follow the same idea as we would behind any .net project e.g single file per class/interface

module Infrastructure {
    export interface IBaseViewModel {
        addOrRemoveClass(selector: string, className: string);
    }
}

我们遇到的问题是 - 所有依赖文件不包括在内?当它被运行时。

the problem we have come across is this - all the dependent files are not being included? when it comes to being run.

如果你看看这个文件是我们应用程序的主要入口点,我在创建一个新的ViewModelBuilder时得到一个未定义的错误

If you look at this file which is the main entry point to our application, I get an undefined error on creating a new ViewModelBuilder

module Infrastructure {
    export class Fusion {
        constructor() {
            this.vmBuilder = new ViewModelBuilder();
            this.setApplicationDefaults();
            this.start();
        }

        private vmBuilder: IViewModelBuilder;

        private start() {
            this.vmBuilder.process();
        }

        private setApplicationDefaults() {
            $.pnotify.defaults.styling = "jqueryui";
            $.pnotify.defaults.history = false;
            $.pnotify.defaults.auto_display = false;
        }
    }
}

位于顶部我们有文件

/// <reference path="../Typings/jquery.d.ts" />
/// <reference path="ViewModelBuilder/ViewModelBuilder.ts" />
/// <reference path="ViewModelBuilder/IViewModelBuilder.ts" />

还有ViewModelBuilder

also ViewModelBuilder

module Infrastructure {

    export class ViewModelBuilder { }
}

所以问题是这样的 - 最好的方法是什么?我们如何让其他类声明工作?我们的项目将会大大增加我们可能会有100个视图模型代表每个视图(项目是从Windows窗体转换为Web)

So the question is this - What is the best way to go on? how can we get other class declarations to work? our project is going to grow considerably we probably 100 view models which will represent each view (the project is a conversion from windows forms to web)

任何帮助都会很棒,因为我们真的需要征服它并继续前进! :)

Any help would be fantastic as we really need to conquer this and move on! :)

推荐答案

我写了为TypeScript做正确的设置,其中一个想法会对你有帮助 - 我最初有这个想法来自Mark Rendle。

I wrote about getting the right set up for TypeScript and one of the ideas in there will help you - I originally got the idea from Mark Rendle.

想法是创建一个名为 references.ts 的文件并列出所有依赖项在那里。有点像这样:

The idea is that create a file named references.ts and have all of your dependencies listed in there. A bit like this:

/// <reference path="modulea.ts" />
/// <reference path="moduleb.ts" />
/// <reference path="modulec.ts" />
/// <reference path="moduled.ts" />
/// <reference path="modulee.ts" />

然后,您可以在所有TypeScript文件的顶部简单地引用此文件:

You can then simply reference this file at the top of all your TypeScript files:

/// <reference path="references.ts" />
module ModuleA {
    export class MyClass {
        doSomething() {
            return 'Hello';
        }
        tester() {
            var x = new ModuleB.MyClass();
        }
    }
}

references.ts 文件就像你在.NET项目中使用的References文件夹一样。

The references.ts file acts like the References folder you have for a .NET project.

另一条建议,对此很有效。设置,是使用 - out 标志来编译单个JavaScript文件,从 app.ts 开始。 TypeScript编译器遍历所有依赖项,并为所有生成的JavaScript计算出正确的顺序。

The other recommendation, which works quite well with this set up, is to use the --out flag to compile a single JavaScript file, starting with app.ts. The TypeScript compiler walks all the dependencies and works out the correct order for all the generated JavaScript.

tsc --out final.js app.ts

此策略将比您的程序更好地扩展,比手动引用任何地方的特定文件。

This strategy will scale with your program much better than manually referencing specific files everywhere.

这篇关于构成Type Script项目的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆