简体   繁体   English

打字稿,无法加载内部模块

[英]Typescript, cant load internal module

I m actually learning typescript, and I m facing some problems with internal modules. 我实际上正在学习打字稿,而我面临内部模块的一些问题。

In fact, I have three files : 实际上,我有三个文件:

index.ts in which I start my app 我在其中启动我的应用程序的index.ts

///<reference path='RouteManager.ts'/>
import RouteManager = RestifyRouting.RouteManager;

var myManager = new RouteManager();
myManager.init("superpath");

RouteManager.ts that manage my REST routes 管理我的REST路由的RouteManager.ts

///<reference path='RouteParser.ts'/>
module RestifyRouting {

    export class RouteManager {

        routeParser:RouteParser;

        constructor() {

        }

        public init(filePath) {
            this.routeParser = new RouteParser();
            this.routeParser.register("zfaf","callback");
            console.log(filePath);
        }
    }

}

RouteParser which has to parse some string to get some informations RouteParser,它必须解析一些字符串以获得一些信息

module RestifyRouting {

    export class RouteParser {

        constructor() {

        }


        public register(path, callback) {
            console.log('super register');
        }

    }

}

I have a gulp file that creates my .js and d.ts files and it works great, except for the index.js file. 我有一个创建我的.js和d.ts文件的gulp文件,除了index.js文件外,它工作得很好。 The compiler tells my that RestifyRouting (which is my internal module) is undefined and I dont know why... 编译器告诉我,RestifyRouting(这是我的内部模块)未定义,我不知道为什么...

Can you help me ? 你能帮助我吗 ?

PS : every files are in the same folder, it's just a learning application. PS:每个文件都在同一个文件夹中,这只是一个学习应用程序。

Thanks for advance 谢谢前进

As of TypeScript 1.5 the module syntax is aligned with ES6 module syntax and that is what I have been using as well... 从TypeScript 1.5开始,模块语法与ES6模块语法保持一致,这也是我一直在使用的语法...

You can remove any references to TypeScript modules and just export the classes directly 您可以删除对TypeScript模块的任何引用,而直接直接导出类

index.ts 索引

import { RouteManager } from './RouteManager';
var myManager = new RouteManager();
myManager.init("superpath");

RouteManager.ts RouteManager.ts

import { RouteParser } from './RouteParser';
export class RouteManager {
    routeParser:RouteParser;
    constructor() {}
    public init(filePath) {
        this.routeParser = new RouteParser();
        this.routeParser.register("zfaf","callback");
        console.log(filePath);
    }
}

RouteParser.ts RouteParser.ts

export class RouteParser {
    constructor() {}
    public register(path, callback) {
        console.log('super register');
    }
}

Keeping modules 保持模块

If you'd like to keep using internal modules then you have to be sure to export your module as well as the classes inside the module. 如果您想继续使用内部模块,则必须确保导出模块以及模块内部的类。

// RouteManager.ts
export module RestifyRouting {
  export class RouteManager{}
}

//index.ts
import { RestifyRouting } from './RouteManager';
//usage
var manager = new RestifyRouting.RouteManager();

Something to keep in mind is that you will not be able to import multiple items into the the same name. 请记住,您将无法将多个项目导入相同的名称。

// i.e.
import { RestifyRouting } from './RouteManager';
import { RestifyRouting } from './RouteParser';

NOTES the {} syntax in the import statement can allow multiple imports 注意import语句中的{}语法可以允许多次导入

{ Class1, Class2 }

The {} can be skipped if you exporting a default: 如果导出默认值,则可以跳过{}

//Source (foo.ts):
export default class Foo{}
//Reference:
import Foo from './foo';
//usage:
class User {
  foo: Foo;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM