如果有导入,则不会触发打字稿自执行功能

时间:2016-06-07 05:03:36

标签: javascript typescript

考虑这个打字稿:

(()=> {
console.log('called boot'); // 'called boot'
})();

结果JS:

(function () {
    console.log('called boot');
})();

define("StockMarketService", ["require", "exports", "jquery"], function(require, exports, $) {
    "use strict";
    var StockMarketService = (function (_super) {
        __extends(StockMarketService, _super);
        ...blah...
        return StockMarketService;
    }(Riot.Observable));
    exports.StockMarketService = StockMarketService;
});

VS Typescript:

import {StockMarketService} from "./services/stockmarket-service";
(()=> {
    console.log('called boot'); //NOTHING HAPPENS.
})();

结果JS:

define("StockMarketService", ["require", "exports", "jquery"], function(require, exports, $) {
    "use strict";
    var StockMarketService = (function (_super) {
        __extends(StockMarketService, _super);
        ...blah...
        return StockMarketService;
    }(Riot.Observable));
    exports.StockMarketService = StockMarketService;
});
define(["require", "exports"], function (require, exports) {
  "use strict";
  (function () {
    console.log('called boot');
  })();
});

在第二个版本中,IFFE不再有效。这是应用程序的入口点。它不是一个模块。我只想执行IFFE。所有依赖项都在一个文件中,我需要引用/使用它们。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

  

在第二个版本中,IFFE不再有效。

第二个例子是模块more on this)。模块主体仅在请求模块时执行。如果从应用程序根目录使用模块(链中的某个位置),模块体将执行。

答案 1 :(得分:-1)

任何js代码都是有效的TypeScript代码,

所以改变

import {StockMarketService} from "./services/stockmarket-service";

(()=> {     console.log('叫做boot'); //什么都没发生。 })();

import {StockMarketService} from "./services/stockmarket-service";

(function(){     console.log('叫做boot'); })();