如何为ngrx / store 2.2.1配置system- config.js.

时间:2016-09-20 11:42:49

标签: javascript angular typescript redux ngrx

我使用Angular 2.0.0并将system- config.js配置如下

/** Map relative paths to URLs. */
const map: any = {
  'app': 'src/app',
  'main': 'main.js',
  '@angular/core': 'vendor/@angular/core/bundles/core.umd.js',
  '@angular/common': 'vendor/@angular/common/bundles/common.umd.js',
  '@angular/compiler': 'vendor/@angular/compiler/bundles/compiler.umd.js',
  '@angular/platform-browser': 'vendor/@angular/platform-browser/bundles/platform-browser.umd.js',
  '@angular/platform-browser-dynamic': 'vendor/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
  '@angular/http': 'vendor/@angular/http/bundles/http.umd.js',
  '@angular/router': 'vendor/@angular/router/bundles/router.umd.js',
  '@angular/forms': 'vendor/@angular/forms/bundles/forms.umd.js',
  'ng2-bootstrap': 'vendor/ng2-bootstrap',
  'ng2-translate': 'vendor/ng2-translate',
  'angular2-fontawesome': 'vendor/angular2-fontawesome',
  '@ngrx': 'vendor/@ngrx',
  'moment': 'vendor/moment/min/moment.min.js'
};

/** User packages configuration. */
const packages: any = {
  'app': {main: 'main', defaultExtension: 'js'},
  'rxjs': {main: 'Rx.js', defaultExtension: 'js'},
  'ng2-bootstrap': { defaultExtension: 'js' },
  'ng2-translate': { defaultExtension: 'js' },
  'angular2-fontawesome': { defaultExtension: 'js' },
  '@ngrx/core': {
    main: 'index.js',
    format: 'cjs'
  },
  '@ngrx/store': {
    main: 'index.js',
    format: 'cjs'
  },
  '@ngrx/effects': {
    main: 'index.js',
    format: 'cjs'
  }
};


////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
 * Everything underneath this line is managed by the CLI.
 **********************************************************************************************/
const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/forms',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',
  // Thirdparty barrels.
  'rxjs',
  // App specific barrels.
  'app/',
  'app/components/',
  'app/components/+home/',
  'app/components/header',
  'app/components/shared',
  'app/accounts/',
  'app/accounts/+login/',
  'app/accounts/+signup/'

  /** @cli-barrel */
];

const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
  cliSystemConfigPackages[barrelName] = { main: 'index' };
});

/** Type declaration for ambient System. */
declare var System: any;

// Apply the CLI SystemJS configuration.
System.config({
  map: {
    '@angular': 'vendor/@angular',
    'rxjs': 'vendor/rxjs',
    'main': 'main.js'
  },
  packages: cliSystemConfigPackages
});

// Apply the user's configuration.
System.config({ map, packages });

但要启动应用程序,我从@ ngrx / store / index.js

收到以下错误
zone.js:344 Unhandled Promise rejection: SyntaxError: Unexpected token export
        at Object.eval (http://localhost:4200/app/app.module.js:14:15)
        at eval (http://localhost:4200/app/app.module.js:53:4)
        at eval (http://localhost:4200/app/app.module.js:54:3)
    Evaluating http://localhost:4200/vendor/@ngrx/store/index.js
    Evaluating http://localhost:4200/app/app.module.js
    Evaluating http://localhost:4200/app/index.js
    Evaluating http://localhost:4200/main.js
    Error loading http://localhost:4200/main.js ; Zone: <root> ; Task: Promise.then ; Value: Error: SyntaxError: Unexpected token export(…) nullconsoleError @ zone.js:344
zone.js:346 Error: Uncaught (in promise): Error: SyntaxError: Unexpected token export(…)

这是我的@ ngrx / store

版本
BISITE@bisite ~/Documents/projects/photogram: npm list @ngrx/store
instangular@0.0.0 /Users/BISITE/Documents/projects/photogram
└── @ngrx/store@2.2.1 

有谁能告诉我目前的映射方式是什么?

app模块如下所示:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
import { StoreModule } from '@ngrx/store';
/* App Root */
import { AppComponent } from './app.component';
/* Feature Modules */
import { AccountsModule } from './accounts/accounts.module';
/* root reducer */
import { appReducer } from './app.reducers';
import { routing, appRoutingProviders } from './app.routing';
/* App declarations */
import { HomeComponent } from './components/+home/';
import { HeaderComponent } from './components/header/';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    HeaderComponent
  ],
  imports: [
    BrowserModule,
    AccountsModule,
    StoreModule.provideStore(appReducer),
    routing,
    Ng2BootstrapModule
  ],
  providers: [
    appRoutingProviders
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

感谢。

3 个答案:

答案 0 :(得分:3)

在程序包配置中,必须将bundle UMD指定为入口点

'@ngrx/core': {
   main: 'bundles/core.min.umd.js',
   format: 'cjs'
 },
 '@ngrx/store': {
   main: 'bundles/store.min.umd.js',
   format: 'cjs'
 }

答案 1 :(得分:1)

在开发人员工具的网络选项卡中,您可以确定ngrx是否已加载,但我认为问题不在于您的system.config.js。对我来说似乎问题出在app.module。[ts | js]。

答案 2 :(得分:0)

将以下行添加到为我修复的systemjs.config.js文件

  '@ngrx/core':'npm:@ngrx/core/bundles/core.umd.js',    
  '@ngrx/store':'npm:@ngrx/store/bundles/store.umd.js', 
  '@ngrx/store-devtools':'npm:@ngrx/store-devtools/bundles/store-devtools.umd.js',