如果在typescript中模块导入期间未指定.js扩展名,则抛出错误404

时间:2016-03-09 21:41:18

标签: django typescript angular systemjs

我关注了angular2教程https://angular.io/docs/ts/latest/tutorial/。 我不希望它在node.js lite-server下运行,但是在django下运行。

我设法完成它并且它的工作但是:每次我导入任何角度模块,例如。在 index.html

System.import('static/dist/js/main')
            .then(null, console.error.bind(console));

main.ts

import {AppComponent} from './app.component'

或者无处不在,javascript应用程序正在尝试加载一些js资源,例如static / dist / js / main文件当然不存在,因为编译后的文件名为 main.js 而不是

当我添加扩展名' .js'像:

System.import('static/dist/js/main.js')
                .then(null, console.error.bind(console));

import {AppComponent} from './app.component.js'

它突然起作用,但ts到js编译器(npm run tsc)抛出错误(即使是编译),我的IDE强调导入为错误。

这是我的完整index.html:

{% verbatim %}
<html>
  <head>
      <base href="/">
    <title>Angular 2 QuickStart</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">    
    <link rel="stylesheet" href="styles.css">

    <!-- 1. Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <script src="static/node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="static/node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="static/node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>

    <script src="static/node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="static/node_modules/systemjs/dist/system.src.js"></script>
    <script src="static/node_modules/rxjs/bundles/Rx.js"></script>
    <script src="static/node_modules/angular2/bundles/angular2.dev.js"></script>
    <script src="static/node_modules/angular2/bundles/router.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('static/dist/js/main')
            .then(null, console.error.bind(console));
    </script>
  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>
{% endverbatim %}

我检查了这个Angular2 Typescript app throws 404 on components when js extension not specified,但它没有帮助我。

有人有想法吗?

1 个答案:

答案 0 :(得分:1)

假设您已配置tsc以将代码输出到&#39; static / dist / js&#39;您应该能够像这样配置Systemjs:

  <script>
        System.defaultJSExtensions = true;
        System.config({
            baseURL: "./static/dist/js"
        });

        System.import("main")
    </script>