未指定 js 扩展时,Angular2 Typescript 应用程序在组件上抛出 404

Angular2 Typescript app throws 404 on components when js extension not specified

我刚开始玩 Angular2,运行 遇到一个奇怪的组件文件扩展问题:

让我们使用5 minutes quickstart demo from angular.io(我将在此处重现代码以供参考)。

文件结构

angular2-quickstart
|- app
|  |- app.component.ts
|  |- boot.ts
|
|- index.html
|- package.json
|- tsconfig.json

package.json

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10"
  },
  "devDependencies": {
    "concurrently": "^1.0.0",
    "lite-server": "^1.3.1",
    "typescript": "^1.7.3"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}

app/app.component.ts

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }

app/boot.ts

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'

bootstrap(AppComponent);

index.html

<html>

  <head>
    <title>Angular 2 QuickStart</title>

    <!-- 1. Load libraries -->
    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>

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

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>

这很有效,但暴露了整个文件结构,包括 node_modules 和配置,这似乎是个坏主意。

所以我尝试只公开 app 文件夹。为此,我执行了以下操作:

当 运行 时,一切正常,除了 angular2-polyfills.js 返回:

Error: XHR error (404 Not Found) loading http://localhost:3000/app.component(…)
  run @ angular2-polyfills.js:138
  zoneBoundFn @ angular2-polyfills.js:111
  lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511
  lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523
  lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494
  lib$es6$promise$$internal$$publishRejection @ angular2-polyfills.js:1444
  (anonymous function) @ angular2-polyfills.js:243
  run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111
  lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305

当然,如果我要求 http://localhost:3000/app.component.js 文件会正确返回。

所以我的问题是:为什么 .js 扩展没有附加到 app.component,导致这个错误,当服务器 baseDir 是项目的根目录时,它在哪里起作用?我错过了一些配置选项吗?

在我的 boot.ts 文件中,我更改了这一行:

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

并且有效。我今天也去Google报道了

您可以配置应用程序文件夹的路径。就我而言,我正在使用 angular 2 来制作我们应用程序的移动版本,并且必须像这样配置系统:

System.config({
            paths: {
                'app/*': './js/mobile/dist/*'
            },
            packages: {        
              app: {
                format: 'register',
                defaultExtension: 'js'
              }
            }
          });
          System.import('app/main')
                .then(null, console.error.bind(console));

如您所见,我决定将 "app" 映射到 "dist",我在其中存放已编译的 js 文件,以将它们与 ts 文件分开。

这个问题可能是 JetBrains WebStorm 在将文件重构到新位置时引起的。它似乎在导入时附加了 .ts 扩展名。

因此,请将新文件保留在原处,但要检查每个文件的导入。

您所有的导入都应该类似于此

import {MyComponent} from './Components/MyComponent';

而不是这个

import {MyComponent} from './Components/MyComponent.ts';

你错过了 app/main.ts。它只有几行,所以在快速入门中很容易错过。

我在 chrome 中遇到了同样的错误,在拔掉我的头发一段时间后,我尝试关闭 adblock,然后它就起作用了。如果你有 adblock,它可能是罪魁祸首

在我的例子中,添加后就解决了module.id

@Component({
    ***moduleId: module.id,***
    providers : [MyService],
    templateUrl: 'sandbox.component.html',
})

在尝试了其他人建议的许多其他解决方案后,我发现它可以通过 dots in filenames 解决,这两个解决方案对我有用。 第一:

defaultJSExtensions: true,

但是defaultJSExtensions is a property that will be deprecated in future 第二个:

packages: {
            '.': {
                defaultExtension: 'js'
            }
        }