我是新的ngrx和阅读从他们的网站ngrx.io的ngrx文档。我在他们的还原器里偶然发现了一些代码,我不明白。
这是counter.actions.ts文件
import { createAction } from '@ngrx/store';
export const increment = createAction('[Counter Component] Increment');
export const decrement = createAction('[Counter Component] Decrement');
export const reset = createAction('[Counter Component] Reset');
这是counter.reducer.ts文件。
import { createReducer, on } from '@ngrx/store';
import { increment, decrement, reset } from './counter.actions';
export const initialState = 0;
const _counterReducer = createReducer(
initialState,
on(increment, (state) => state + 1),
on(decrement, (state) => state - 1),
on(reset, (state) => 0)
);
export function counterReducer(state, action) {
return _counterReducer(state, action);
}
这是app模块文件
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { StoreModule } from '@ngrx/store';
import { counterReducer } from './counter.reducer';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, StoreModule.forRoot({ count: counterReducer })],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
我的问题如下:
export function counterReducer(state, action) {
return _counterReducer(state, action);
}
在减速机的档案里?
我在ngrx网站上阅读有关行动的文章时看到了这些。
导出的还原器函数是必需的,因为函数调用不支持View Engine AOT编译器。如果您使用默认的Ivy AOT编译器(或JIT),则不再需要它。
这是一个可能的解释吗?
发布于 2021-08-24 03:38:54
我相信下面的方法是有效的,没有真正的意义通过另一个函数进行再导出,因为这并没有真正做任何事情。
import { createReducer, on } from '@ngrx/store';
import { increment, decrement, reset } from './counter.actions';
export const initialState = 0;
const counterReducer = createReducer(
initialState,
on(increment, (state) => state + 1),
on(decrement, (state) => state - 1),
on(reset, (state) => 0)
);
发布于 2021-08-24 03:59:52
1,2)。你的还原器必须导出约简方法/功能。您可以像@wscttc建议的那样导出它。如果还原器返回一个更复杂的对象,甚至可以为响应添加一个类型。
import { createReducer, on } from '@ngrx/store';
import { increment, decrement, reset } from './counter.actions';
const initialState = 0;
export const counterReducer = createReducer<number>(
initialState,
on(increment, (state) => state + 1),
on(decrement, (state) => state - 1),
on(reset, (state) => 0)
);
3)。其主要思想是具有特征状态。随着应用程序的增长,使用自己的还原剂、效果、动作的几个特性状态将是明智的。
https://stackoverflow.com/questions/68905207
复制相似问题