为什么我的Typescript导入仅在我执行" console.log(myImport)"?

时间:2016-03-22 20:10:19

标签: angularjs import typescript jspm

我尝试使用JSPM 1.5.2加载我的Angular 0.17.0应用(使用Typescript编写)。

这是我的HTML:

<!DOCTYPE html>
<html ng-app="MyApp">
  <head>
    <meta charset="utf-8">
    <title>My App</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">

    <link rel="stylesheet" href="index.css">

    <script src="jspm_packages/system.js"></script>
    <script src="jspm.browser.js"></script>
    <script src="jspm.config.js"></script>
  </head>
  <body>
    <main first-name="huh" last-name="what"></main>
    <script>
      SystemJS.import('app.ts');
    </script>
  </body>
</html>

对于我的./app.ts此操作&#39; T工作↓

import $ from 'jquery'
import angular from 'angular'
import main from './components/main/main.ts';

module app {

    angular.module('MyApp', [
        'main'
    ])

}

// which gives me "[$injector:nomod] Module 'main' is not available!"

这项工作↓

import $ from 'jquery'
import angular from 'angular'
import main from './components/main/main.ts';

module app {

     angular.module('MyApp', [
        'main'
    ])

    console.log(main);

}

// Now it works fine, no error.

那么为什么这会失败,除非我在模块声明中用console.log(main)显式引用我的导入模块?我的导入语法有什么问题?

1 个答案:

答案 0 :(得分:1)

为避免触发副作用,imports are not loaded until referenced in a module

归功于Ryan Cavanaugh