systemjs.config.js 不将 *.js 附加到文件名



我在 app.module 的 Chrome 开发者控制台中找不到 404 .js因为尝试解析的 URL,在第一次输入要索引的 URL 后.html,如下所示,请注意 app.module 缺少.js扩展名:

http://localhost:8080/someTomcatContext/scripts/someAppName/src/app/app.module

以下是文件夹结构:

node_modules
package.json
styles.css
systemjs.config.extras.js
systemjs.config.js
systemjs-angular-loader.js
tslint.json
src
|-- app
|   |-- app.component.ts
|   |-- app.module.ts
|   |-- app.routes.ts
|-- index.html
|-- main.ts
|-- tsconfig.json

我有以下系统配置.js

(function (global) {
  System.config({
    paths: {
  // paths serve as alias
  'npm:': '../node_modules/'
},
// map tells the System loader where to look for things
map: {
  // our app is within the app folder
  'app': 'src/app',
  // angular bundles
  '@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/forms': 'npm:@angular/forms/bundles/forms.umd.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);

我尝试使用以下两种变体更改通配符,以便在 system.config 中附加.js扩展名.js如上所示:

  './src/app/*.js'
  './app/*.js'

以下是索引的内容.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Angular QuickStart</title>
    <script>document.write('<base href="' + document.location + '" />');
</script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../styles.css">
    <!-- Polyfill(s) for older browsers -->
    <script src="../node_modules/core-js/client/shim.min.js"></script>
    <script src="../node_modules/zone.js/dist/zone.js"></script>
    <script src="../node_modules/systemjs/dist/system.src.js"></script>
    <script src="../systemjs.config.js"></script>
    <script>
      System.import('main.js').catch(function(err){ console.error(err); });
    </script>
  </head>
  <body>
    <my-app>Loading AppComponent content here ...</my-app>
  </body>
</html>

以下是主要内容:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
我将 Angular

2 应用程序升级到 Angular 4 应用程序,并使用快速入门项目 (https://github.com/angular/quickstart.git( 作为框架。

我将特定于应用程序的 TS 文件放在 src/app 文件夹中,并使用以下结构使其工作:

node_modules
package.json
tslint.json
src
|-- app
|   |-- app.component.ts
|   |-- app.module.ts
|   |-- app.routes.ts
|-- index.html
|-- main.ts
|-- styles.css
|-- systemjs.config.extras.js
|-- systemjs.config.js
|-- systemjs-angular-loader.js
|-- tsconfig.json

我的 systemjs.config.js 看起来像这样:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': '../node_modules/'
    },
    // 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.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/forms': 'npm:@angular/forms/bundles/forms.umd.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);

我的索引.html看起来像这样:

<!DOCTYPE html>
<html>
  <head>
    <title>Angular QuickStart</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">
    <!-- Polyfill(s) for older browsers -->
    <script src="../node_modules/core-js/client/shim.min.js"></script>
    <script src="../node_modules/zone.js/dist/zone.js"></script>
    <script src="../node_modules/systemjs/dist/system.src.js"></script>
    <script src="systemjs.config.js"></script>
    <script>
      System.import('main.js').catch(function(err){ console.error(err); });
    </script>
  </head>
  <body>
    <my-app>Loading AppComponent content here ...</my-app>
  </body>
</html>

main.ts 看起来像这样:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);

抱歉,这可能来得太晚了,但我希望它对其他人有所帮助。我在角度快速入门项目中遇到了同样的问题。事实证明,您需要在systemjs.config.js文件中向System.config添加一个defaultJSExtensions标志,并将其设置为true。

System.config(
    //paths and other settings...
    defaultJSExtensions : true
)(this);

请参阅github上的问题 https://github.com/systemjs/systemjs/issues/812

最新更新