angular - 使用 .ts 文件时出现 404(未找到)错误

标签 angular typescript

当我在生产环境中加载页面时,出现以下错误:

GET http://example.com/Scripts/main.ts 404 (Not Found) error: (SystemJS) XHR error (404 Not Found) loading http://example.com/Scripts/main.ts

不会发生在我的开发者身上。环境

这发生在我的 index.html 文件中。我验证了,所有路径都是正确的,main.ts文件存在于路径中。

<script src="/Scripts/systemjs.config.js"></script>
<script>
      System.import('/Scripts/main.ts').catch(function(err){ console.error(err); });
</script>

如果我将 main.ts 文件更改为 main.js 扩展名,则不会出现 no 404 错误。

这是我的 systemjs.config.js 文件:

(function (global) {
  System.config({
    transpiler: 'ts',
    typescriptOptions: {
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "lib": ["es2015", "dom"],
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true
    },
    meta: {
      'typescript': {
        "exports": "ts"
      }
    },
    paths: {
      'npm:': 'https://unpkg.com/'
    },
    map: {
      app: 'app',
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',

      'rxjs':                      'npm:rxjs@5.0.1',
      'ts':                        'npm:plugin-typescript@5.2.7/lib/plugin.js',
      'typescript':                'npm:typescript@2.0.10/lib/typescript.js',

    },

    packages: {
        app: {
            format: 'register',
        main: '/Scripts/main.ts',
        defaultExtension: 'ts'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });

})(this);

最佳答案

您的浏览器不理解 TypeScript,只有 TypeScript 编译器可以。 TypeScript 编译器的输出是 JavaScript,因此 main.js 是要导入的正确文件。

关于angular - 使用 .ts 文件时出现 404(未找到)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42412104/

相关文章:

javascript - 带有排斥圆圈的圆图

node.js - 从 Angular 6 应用程序向 Elasticsearch 发送 get 请求时出现 CORS 错误,即使允许

jquery - 进入angular8中动态列/行mat-table的单元格后如何获取每个单元格的数据?

node.js - 将 Node 模块导入 typescript/systemjs

javascript - 在 JavaScript/TypeScript 中迭代 Json 数据

javascript - 过渡 CSS3 在 Angular2 中不起作用?

javascript - Typescript AngularJS 'this' 未定义

javascript - 在其他地方声明时使用祖先模块中的变量

javascript - 通过 ngSubmit 提交时未定义 Angular 形式

angular - Material 自动完成面板问题