简体   繁体   中英

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

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

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

RouteManager.ts that manage my REST routes

///<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

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. The compiler tells my that RestifyRouting (which is my internal module) is undefined and I dont know why...

Can you help me ?

PS : every files are in the same folder, it's just a learning application.

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...

You can remove any references to TypeScript modules and just export the classes directly

index.ts

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

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

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

{ 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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