1

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

我正在为 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;
        }
    }
}

那么,我错过了什么?

4

1 回答 1

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;
        }
    }
}
于 2013-02-03T01:25:47.163 回答