部署 Angular 2 个应用程序后出错

Error after deploy Angular 2 app

我正在尝试将示例 Angular 2 应用程序部署到我的主机中。我已经完成了以下所有操作:https://angular.io/guide/deployment 并且当我在本地主机上 运行 这个应用程序时一切正常。

但是当我将所有文件都托管时,浏览器控制台出现错误:

Error: (SystemJS) Unexpected token <
SyntaxError: Unexpected token <
    at eval (<anonymous>)
    at ZoneDelegate.invoke (https://unpkg.com/zone.js@0.8.4?main=browser:365:26)
    at Zone.run (https://unpkg.com/zone.js@0.8.4?main=browser:125:43)
    at https://unpkg.com/zone.js@0.8.4?main=browser:760:57
    at ZoneDelegate.invokeTask (https://unpkg.com/zone.js@0.8.4?main=browser:398:31)

这是我的 index.html 文件:

<!DOCTYPE html>
<html>
  <head>
    <!-- Doesn't load from node_modules! -->

    <!-- Set the base href -->
    <base href="/">
    <title>Simple Deployment</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <!-- Polyfills -->
    <script src="https://unpkg.com/core-js/client/shim.min.js"></script>

    <!-- Update these package versions as needed -->
    <script src="https://unpkg.com/zone.js@0.8.4?main=browser"></script>
    <script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js">    </script>

    <!-- This SystemJS configuration loads umd packages from the web -->
    <script src="systemjs.config.server.js"></script>

    <script>
      System.import('main.js')
            .catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>loading...</my-app>
  </body>

</html>

和systemjs.config文件:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      'npm:': 'https://unpkg.com/' // path serves as alias
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      'app': 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.min.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.min.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.min.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.min.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.min.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.min.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.min.js',
      '@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.min.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.min.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.min.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.min.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
          './*.js': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

我尝试更改这些文件,但仍然没有效果。当我从 index.html 文件中删除这些行时,我没有收到任何错误,但页面当然没有加载:

<script src="systemjs.config.server.js"></script>

<script>
  System.import('main.js')
        .catch(function(err){ console.error(err); });
</script>

好的,我找到了解决这个问题的方法。

我将 index.html 文件移动到根文件夹并更改了基本 href:

<base href="/src/app">