NGRX angular 5减速机配AOT编译器

时间:2018-05-17 06:59:01

标签: angular typescript ngrx angular2-aot

我很难将NGRX存储从JIT编译器重写为AOT编译器。 const accountReducer应该是一个导出的函数,但我无法弄清楚如何修复它。

是否有一个简单的重写示例,或者有人可以帮我解决这个问题吗?

错误

  

'accountReducer'包含app / store / account / account.reducer.ts(19,64)中的错误             考虑将函数表达式更改为导出函数。

我有以下设置:

app.module.ts

import { BrowserModule } from '@angular/platform-browser';

import { LOCALE_ID, NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

// NGRX store
import { StoreModule,   ActionReducer } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { rootReducer } from 'store';

// Core
import { environment } from 'environments/environment';
import { AppComponent } from './app.component';

// Routes
import { AppRoutingModule } from 'routes';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        BrowserModule,
        HttpClientModule,
        StoreModule.forRoot(rootReducer), !environment.production ? StoreDevtoolsModule.instrument({
            maxAge: 10,
        }) : [],
        AppRoutingModule,
    ],
    providers: [],
    bootstrap: [AppComponent],
})
export class AppModule {}

root.reducer.ts

import { ActionReducerMap } from '@ngrx/store';
import { routerReducer } from '@ngrx/router-store';

import { initialAccountState, accountReducer } from './account/account.reducer';
import { AccountInterface } from './account/account.interface';

export interface StateInterface {
    account: AccountInterface;
    router: any;
}

// Store root state
export const InitialState = {
    account: initialAccountState,
};

// Store root reducer
export const rootReducer: ActionReducerMap<StateInterface> = {
    account: accountReducer,
    router: routerReducer,
};

account.reducer.ts

import { ActionReducer, Action } from '@ngrx/store';
import { tassign } from 'tassign';

import { saveState, rehydrateState, rehydrateAction } from 'helpers';
import { DefaultAction } from 'interfaces';

import { AccountConstants } from './account.constants';
import { AccountInterface } from './account.interface';

export const initialAccountState: AccountInterface = {
    account: null,
    hash: {
        isLoading: true,
        isAccepted: false,
    },
    isLoggedIn: false,
};

export const accountReducer: ActionReducer<AccountInterface> = (state: AccountInterface = initialAccountState, action: DefaultAction) => {
    const { type, payload } = action;
    let newState: any = state;

    switch (type) {
        case AccountConstants.ACTION_ACCOUNT_LOGOUT_ACCOUNT:
            newState = tassign(initialAccountState);
            break;

        case AccountConstants.ACTION_HASH_DENIED:
            newState = tassign(state, {
                hash: {
                    isLoading: false,
                    isAccepted: false,
                },
            });
            break;

        case AccountConstants.ACTION_HASH_ACCEPTED:
            newState = tassign(state, {
                hash: {
                    isLoading: false,
                    isAccepted: true,
                },
            });
            break;

        // Optional: if the state requires saving storage.
        case rehydrateAction:
            const rehydratedState = rehydrateState('account');
            newState = tassign(state, rehydratedState);
            break;
    }

    // If it's not default init.
    if (type !== rehydrateAction) {
        // Save the whole store, keys or whatever you want :-)
        saveState('account', newState);
    }
    return newState;
};

1 个答案:

答案 0 :(得分:1)

将reducer声明更改为函数:

@IgnoreIf({ !(System.getProperty('execution.environment') in ['staging', 'prod']) })

您编写的方式曾经用于工作,但需要针对新版本更新到上面。

如果您在版本4 this migration之前从NgRx升级可能有用。