TypeScript模块,包含不同文件中的类

时间:2013-02-02 21:18:02

标签: typescript

我正在尝试将模块拆分为多个文件。我遵循了这个question,但没有运气,同一个模块中但在不同文件中的类似乎是我无法访问的。

我正在使用Visual Studio 2012的TypeScript 0.8.2插件

/* filesystem */
- /core
- - Router.ts
- - Server.ts
- /DefinitelyTyped
- - ..
- index.ts


/* file Router.ts */
///<reference path='../DefinitelyTyped/node/node.d.ts'/>
import Url = module("url");

module Core {
    export class Router {
        static route(url: string) {
            var pathname: string = Url.parse(url).pathname;
            var query: string = Url.parse(url).query;
            console.log("About to route to " + pathname);
        }
    }
}

/* file Server.ts */
///<reference path='../DefinitelyTyped/node/node.d.ts'/>
///<reference path="Router.ts"/>

import Http = module("http");

module Core {
    export class Server {

        private server: Http.Server;
        private port: number;

        constructor(port: number) {
            this.port = port;
            this.server = Http.createServer(this.onRequest);
            this.server.listen(this.port);
            console.log("Server started at http://127.0.0.1:" + this.port + "/.");
        }

        private onRequest(request: Http.ServerRequest, response: Http.ServerResponse) {
            var url: string = request.url;

            console.log("Received request: \"" + request.url + "\".");

            Router.route(url); //problem: Name 'Router' doesn't exist in this scope...

            response.writeHead(200, { "Content-Type": "text/plain" });
            response.write(url);
            response.end();
        }

        public getPort(): number {
            return this.port;
        }
    }
}

那么,我错过了什么?

1 个答案:

答案 0 :(得分:4)

作为一般规则,永远不要将import / export/// <reference>标签混合并匹配到包含除声明之外的任何内容的文件(例如,如果您只引用.d.ts个文件在代码中的任何位置使用import / export。你想要的是这个(见NB添加/删除):

/* file Router.ts */
///<reference path='../DefinitelyTyped/node/node.d.ts'/>
import Url = module("url");

// NB Removed containing module for clarity
export class Router {
    static route(url: string) {
        var pathname: string = Url.parse(url).pathname;
        var query: string = Url.parse(url).query;
        console.log("About to route to " + pathname);
    }
}

/* file Server.ts */
///<reference path='../DefinitelyTyped/node/node.d.ts'/>
// NB Removed reference

import Http = module("http");
import Router = module("./Router"); // NB Added

module Core {
    export class Server {

        private server: Http.Server;
        private port: number;

        constructor(port: number) {
            this.port = port;
            this.server = Http.createServer(this.onRequest);
            this.server.listen(this.port);
            console.log("Server started at http://127.0.0.1:" + this.port + "/.");
        }

        private onRequest(request: Http.ServerRequest, response: Http.ServerResponse) {
            var url: string = request.url;

            console.log("Received request: \"" + request.url + "\".");

             // NB first 'Router' is the import name at the top of this file, feel free to change
             // NB second'Router' is the name of the class from that module
            Router.Router.route(url);

            response.writeHead(200, { "Content-Type": "text/plain" });
            response.write(url);
            response.end();
        }

        public getPort(): number {
            return this.port;
        }
    }
}