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

时间:2017-05-16 00:54:58

标签: angular systemjs

我在Chrome开发者控制台中找到了app not文件中的404 Not Found,因为在首次输入index.html的URL后,尝试解析的URL就是这样,注意到该应用.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

我有以下system.config.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);

我尝试使用以下两种变体更改通配符,以便将.js扩展名附加到上面显示的system.config.js中:

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

以下是index.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);

2 个答案:

答案 0 :(得分:1)

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

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

*/

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

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

我的index.html看起来像这样:

/**
 * 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);

main.ts看起来像这样:

<!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>

答案 1 :(得分:1)

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

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

请参阅github https://github.com/systemjs/systemjs/issues/812

上的问题