index.js 没有做 index.ts 做的事

index.js not doing what index.ts does

我目前正在使用 angular2 和打字稿构建一个网络应用程序。我尝试使用 index.ts 所以在我的代码中,我可以简单地使用

import {LayoutComponent} from '../layout';

但是当我转译时,javascript 引擎不会寻找 index.js,而是寻找 ./layout,所以它不起作用。

是否有 typescript 转译器的配置,我应该使用 systemjs 或使用其他加载器(如 webpack)进行配置?

是的,需要配置 systemjs :

您的 index.html 文件中应该有这样的内容:

  System.config({
    packages: {
      src: {
        format: 'register',
        defaultExtension: 'js'
      }
    }
  });
    System.import('src/dist/boot') --- your path to root file
        .then(null, console.error.bind(console));

在 Angular2 文档页面上,您可以找到 five minutes quick start tutorial, follow that one and after that, you can customize your app how you want: https://angular.io/guide/quickstart

我找到了原因。打字稿正在使用此处解释的 nodejs 模块解析: http://www.typescriptlang.org/docs/handbook/module-resolution.html

但是systemjs没有使用nodejs解析。所以我需要使用 ./layout/index 来与 systemjs 模块解析兼容,或者为每个模块使用一个映射条目。但是 commonjs 确实使用 nodejs 解析,所以我要转译到 commonjs 并使用 webpack。