【发布时间】:2018-10-27 08:31:06
【问题描述】:
我在将 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;
};
【问题讨论】:
-
我不知道你的问题的答案,但也许这个 github 问题可能会对你有所帮助。 github.com/ngrx/platform/issues/145
标签: angular typescript ngrx angular2-aot