当未指定js扩展名时,Angular2 Typescript app会在组件上抛出404

xle*_*ier 22 javascript polyfills typescript angular

我刚刚开始使用Angular2,并遇到了一个奇怪的组件文件扩展问题:

让我们使用angular.io5分钟快速入门演示(我将在这里重现代码以供参考).

文件结构

angular2-quickstart
|- app
|  |- app.component.ts
|  |- boot.ts
|
|- index.html
|- package.json
|- tsconfig.json
Run Code Online (Sandbox Code Playgroud)

的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

应用程序/ app.component.ts

import {Component} from 'angular2/core';

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

应用程序/ boot.ts

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

bootstrap(AppComponent);
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

这就像一个魅力,但暴露整个文件结构,包括node_modules和配置,这似乎是一个坏主意.

所以我试图只暴露app文件夹.为此,我做了以下事情:

  • 复制文件app/scripts夹中引用的javascript文件
  • 移动index.htmlapp
  • 更改此文件中的引用路径
  • lite脚本更改package.jsonlite-server --baseDir 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
Run Code Online (Sandbox Code Playgroud)

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

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

hea*_*nds 15

您可以配置app文件夹的路径.在我的情况下,我使用角度2来生成我们的应用程序的移动版本,并且必须像这样配置系统:

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

正如你所看到的,我决定将"app"映射到"dist",在那里我有我编译的js文件,以使它们与ts文件分开.


Ale*_*ins 7

在将文件重构到新位置时,JetBrains WebStorm可能会导致此问题.它似乎在进口上附加了.ts扩展名.

因此,将新文件保存在其位置,但检查每个文件的导入.

你的所有进口都应该像这样

import {MyComponent} from './Components/MyComponent';
Run Code Online (Sandbox Code Playgroud)

而不是这个

import {MyComponent} from './Components/MyComponent.ts';
Run Code Online (Sandbox Code Playgroud)