NGRX угловой 5 редуктор с компилятором AOT - PullRequest
0 голосов
/ 17 мая 2018

Мне трудно переписать хранилище NGRX из JIT-компилятора в AOT-компилятор.Const accountReducer должна быть экспортированной функцией, но я не могу понять, как это исправить.

Есть ли простой пример переписывания, или кто-нибудь может мне помочь с этим?

error

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 голосов
/ 17 мая 2018

Измените объявление редуктора на функцию:

export function accountReducer(
    state: AccountInterface = initialAccountState,
    action: DefaultAction) {
     ...
}

То, как вы написали, использовалось для работы, но для новых версий его необходимо обновить до приведенного выше.

Если вы обновляетеиз NgRx до версии 4 эта миграция может быть полезна.

...