gpt4 book ai didi

Angular 6/NGRX 组合 reducer

转载 作者:太空狗 更新时间:2023-10-29 17:34:35 24 4
gpt4 key购买 nike

我正在使用带 NgRX 4 的 Angular 6。我有多个 reducer ,我想结合起来。

app.module.ts

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

import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';

import { StoreDevtoolsModule } from '@ngrx/store-devtools';

import { AppComponent } from './app.component';
import counterEffects from './store/counter/counter.effects';

import reducers from './store/reducers';

@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
StoreModule.forRoot(reducers),
EffectsModule.forRoot([counterEffects]),
StoreDevtoolsModule.instrument({
maxAge: 10,
}),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}

reducers.ts

import { combineReducers } from '@ngrx/store';
import { reducer as counterReducer, key as counterKey } from './counter';
import { reducer as profileReducer, key as profileKey } from './profile';

const appReducer = combineReducers({
[counterKey]: counterReducer,
[profileKey]: profileReducer,
});

export default (state, action) => {
if (action.type === 'REDIRECT_TO_EXTERNAL') {
state = undefined;
}

return appReducer(state, action);
};

我的 reducer 是标准 reducer ,没有什么特别之处。

来自 React/Redux 背景,我会像这样设置多个 reducer,但是在 Angular 中,当我尝试从商店中选择时,我得到了未定义。当我尝试使用开发工具查看商店时,我看不到任何缩减器,状态只是 {}

如何在 Angular 6/NgRX 4 中设置多个 reducer ?

最佳答案

import { ActionReducerMap } from '@ngrx/store';
import { reducer as counterReducer, key as counterKey } from './counter';
import { reducer as profileReducer, key as profileKey } from './profile';

export interface IAppState {
[counterKey]: any;
[profileKey]: any;
}

export const reducers: ActionReducerMap<IAppState> = {
[counterKey]: counterReducer,
[profileKey]: profileReducer,
};

关于Angular 6/NGRX 组合 reducer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com