Angular2打字稿应用抛出的组件404没有指定JS扩展时 [英] Angular2 Typescript app throws 404 on components when js extension not specified

查看:175
本文介绍了Angular2打字稿应用抛出的组件404没有指定JS扩展时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始玩弄Angular2,跑进一个奇怪的组件文件扩展名问题:

I just started to play around with Angular2, and ran into a weird component file extension problem:

让我们使用5分钟快速入门演示从angular.io (I'这里会重现code仅供参考)。

Let's use the 5 minutes quickstart demo from angular.io (I'll reproduce the code here for reference).

文件结构

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.component.ts

import {Component} from 'angular2/core';

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

应用/ 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 和配置,这似乎是一个坏主意。

This works like a charm, but exposes the whole file structure, including node_modules and the configuration, which seems like a bad idea.

于是,我就仅暴露应用文件夹中。要做到这一点,我做了以下内容:

So I tried to expose only the app folder. To achieve that, I did the following:


  • 所引用的JavaScript文件复制在应用程序/脚本文件夹

  • 将在 index.html的应用

  • 更改该文件中引用的路径

  • 的package.json 更改精简版脚本精简版服务器--basedir应用

  • Copy the referenced javascript files in an app/scripts folder
  • Move the index.html in app
  • Change the references paths in this file
  • Change the lite script in package.json to lite-server --baseDir app

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

When running this, everything worked, except that angular2-polyfills.js returned :

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://本地主机:3000 / app.component.js 文件正确返回

所以我的问题是:为什么不的.js 扩展获得追加到 app.component ,导致在此错误,如该工作在服务器BASEDIR是该项目的根源在哪里?我错过了一些配置选项?

So my question is: why doesn't the .js extension get appended to app.component, resulting in this error, where that worked when the server baseDir was the root of the project ? Did I miss some configuration option ?

推荐答案

您可以配置路径,您的应用程序文件夹中。就我而言,我使用的角2来生产我们的应用程序的移动版本,不得不配置系统,像这样:

You can configure the path to your app folder. In my case, I'm using angular 2 to produce a mobile version of our app and had to configure System like so:

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

正如你所看到的,我决定地图应用到DIST在那里我有我的编译的JS文件,让他们从TS文件分开。

As you can see, I decided to map "app" to "dist" where I have my compiled js files to keep them separate from the ts files.

这篇关于Angular2打字稿应用抛出的组件404没有指定JS扩展时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆