Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从TypeScript中的文件加载类_Typescript - Fatal编程技术网

无法从TypeScript中的文件加载类

无法从TypeScript中的文件加载类,typescript,Typescript,我有一个类似这样的类: export module GameModule { export class Game { private boardContainer: HTMLElement; private board: number[][]; constructor (container: HTMLDivElement) { this.boardContainer = container;

我有一个类似这样的类:

export module GameModule {
    export class Game {
        private boardContainer: HTMLElement;
        private board: number[][];

        constructor (container: HTMLDivElement) {
            this.boardContainer = container;
            this.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];

            this.drawGrid();
        }

        drawGrid() {

        }
    }
}
以及主要应用程序:

import GameModule = module("./GameModule");

window.onload = () => {
    new GameModule.GameModule.Game(<HTMLDivElement> document.getElementById('content'));
};

我的代码怎么了?

你的代码对我来说编译得很好。以下是我所拥有的:

GameModule.ts

export module GameModule {
    export class Game {
        private boardContainer: HTMLElement;
        private board: number[][];

        constructor (container: HTMLDivElement) {
            this.boardContainer = container;
            this.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];

            this.drawGrid();
        }

        drawGrid() {

        }
    }
}
import GameModule = module("./GameModule");

window.onload = () => {
    new GameModule.GameModule.Game(<HTMLDivElement> document.getElementById('content'));
};
Main.ts

export module GameModule {
    export class Game {
        private boardContainer: HTMLElement;
        private board: number[][];

        constructor (container: HTMLDivElement) {
            this.boardContainer = container;
            this.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];

            this.drawGrid();
        }

        drawGrid() {

        }
    }
}
import GameModule = module("./GameModule");

window.onload = () => {
    new GameModule.GameModule.Game(<HTMLDivElement> document.getElementById('content'));
};

编译器报告没有问题。你能检查一下你是否能准确编译这两个类吗?此外,请张贴您的完整代码(如果可能)。从错误消息中,我假设您的模块位于另一个目录中。

Hi@skayred!您尝试加载模块的第一个文件的名称是什么?第一个文件的名称必须是“GameModule.ts”。你能验证一下吗?我的主要类是names
app.ts
,GameModule在
GameModule.ts
中。我的名字有问题吗?@skayred我刚注意到你编译了app.tss。你的问题中有错吗<代码>游戏模块。ts是正确的名称。您正在使用外部模块,因此模块导入需要包含模块的文件的相对名称。我再次复制了此错误。只需创建新的VS2012 TypeScript项目,将
app.js
内容更改为my并创建
GameModule.ts
。当你试图编译它时,你会得到一个错误。有必要输入两次GameModule吗?(就像在新的GameModule.GameModule.Game中一样…@stepancheg它曾经是。我不确定最新版本的编译器是否仍然如此,但我想是这样。