angular2 rc6 - 使用子文件夹/ src / app的systemjs.config.js

时间:2016-09-03 02:23:46

标签: javascript angular typescript systemjs

在angular2快速启动页面上: https://angular.io/guide/quickstart

使用Angular2 rc6,文件systemjs.config.js已更改。我正在尝试将文件夹/ app /更改为/ src / app的角度快速启动符合我的应用程序,所以我不确定我必须在此文件中更改。我已经尝试了一些路径,但它不起作用,任何人都可以解释更多关于这个文件以及我如何使用路径。在这种配置中,我不关心生产或/ dist,因为我只是测试角度。 我之前正在使用webpack starters或angular-cli,但新版本的角度打破了这些包,所以我想保持简单。

的src /应用/ systemjs.config.js:

/**
 * System configuration for Angular 2 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',
      // angular testing umd bundles
      '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
      '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
      '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
      '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
      '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
      '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
      '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
      '@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js',
      // other libraries
      'rxjs':                       'npm:rxjs',
      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      'angular2-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

的src /应用程序/ app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule }     from '@angular/http';

import { AppComponent } from './app.component';
import { Configuration } from './app.constants';
import { routing }        from './app.routing';
import { AccountListComponent } from './accounts/account-list/account-list.component';
import { AccountDetailComponent } from './accounts/account-detail/account-detail.component';
import { DashboardComponent } from './dashboard/';


@NgModule({
  declarations: [
    AppComponent,
    AccountListComponent,
    AccountDetailComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
    HttpModule,
    routing
  ],
  providers: [Configuration],
  entryComponents: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {

}

的src / app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
export class AppComponent {
  title = 'app works!';
}

/src/main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule, environment } from './index';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);

./的index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Ng2</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" type="text/css" href="vendor/bootstrap/dist/css/bootstrap.min.css">
  <base href="/">
</head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

我从angular发现新的systemjs.config有点混乱。这是我使用的,它很有用:

(function(global) {
    var map = {
            'app': 'src/app',
            '@angular': 'node_modules/@angular',
            'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
            'rxjs': 'node_modules/rxjs'
        },
        packages = {
            'app': { main: 'main.js',  defaultExtension: 'js' },
            'angular2-in-memory-web-api': {main: './index.js',defaultExtension:'js'}
            'rxjs': { defaultExtension: 'js' }
        },
        ngPackageNames = [
            'common',
            'compiler',
            'core',
            'http',
            'platform-browser',
            'platform-browser-dynamic',
            'router',
            'forms'
        ],

        tests = [
            'core/testing',
            'common/testing',
            'compiler/testing',
            'platform-browser/testing',
            'platform-browser-dynamic/testing',
            'http/testing',
            'router/testing',
            'forms/testing'
        ];

    function packUmd(pkgName) {packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.min.js', defaultExtension: 'js' }}

    ngPackageNames.forEach(packUmd);

    tests.forEach(function (test) {
        return packages['@angular/'+ test] = { main: '/bundles/' + test.replace(/\//, '-') + '.umd.min.js', defaultExtension: 'js' }
    })

    var config = {
        map: map,
        packages: packages
    };

    System.config(config);

})(this);

我认为这看起来更清晰易懂。如果您的应用文件夹更改,您唯一需要更改的内容是:'app': 'path-to-app/app'

答案 1 :(得分:0)

要在NodeJS中加载模块,请使用以下命令安装SystemJS

npm install systemjs

如果是转换ES模块,请按照转换程序项目页面中的说明安装transpiler插件。