简体   繁体   English

打字稿AMD模块没有返回任何内容

[英]Typescript AMD Module not returning anything

I am exporting a simple function inside of a "log.ts" file: 我正在“log.ts”文件中导出一个简单的函数:

export function message(s : string) {
    console.log(s);
}

This is imported by a file ("MyController.ts") in the same directory: 这是由同一目录中的文件(“MyController.ts”)导入的:

import log = module("./log");
class MyController {

    a : string = "aaa";

    constructor () {
        log.message("hello world");
    }
}

When compiled, I get the following JS: 编译时,我得到以下JS:

define(["require", "exports", "./log"], function(require, exports, __log__) {
    var log = __log__;

    var MyController = (function () {
        function MyController() {
            this.a = "aaa";
            log.message("hello world");
        }
        return MyController;
    })();    
})
//@ sourceMappingURL=MyController.js.map

This define function should return MyController. 这个define函数应该返回MyController。 Because it does not, the callback inside this snippet does not get anything for the controller parameter: 因为它没有,此代码段内的回调不会获得控制器参数的任何内容:

   require(["MyController"], function (controller) {
                    theRoute.controller = controller;
                    defer.resolve();
                    $rootScope.$apply();
                });

I can fix this by manually adding the return inside of the call to define, but this is not a good workaround because the JS is being outputted by the TS compiler. 我可以通过手动添加调用内部的返回来修复此问题,但这不是一个好的解决方法,因为JS正由TS编译器输出。

Am I doing something wrong or is this a bug in typescript? 我做错了什么,或者这是打字稿中的错误?

You should write: 你应该写:

import log = module("./log");
export class MyController { // <--- 'export'
    a : string = "aaa";
    constructor () {
        log.message("hello world");
    }
}

And: 和:

   require(["MyController"], function (controller) {
     theRoute.controller = new controller.MyController(); // <--
     defer.resolve();
     $rootScope.$apply();
   });

Starting in 0.9.x you'll be able to write export = MyController; 从0.9.x开始,你将能够编写export = MyController; at the bottom of the .ts file to make the class be the top-level exported object. 在.ts文件的底部,使该类成为顶级导出对象。

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

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