为什么 ngrx 忽略我的初始商店价值
Why is ngrx ignoring my initial store value
我有这个使用 Ngrx 的基本 Todo 应用程序,但我正在努力让它工作。我无法获得初始状态,即要显示的 TodoItems 数组。
动作:
import { Action } from "@ngrx/store";
import { ToDoItem } from "./models";
export enum ToDoActions {
Add = 'Add customer' ,
Remove = 'Remove customer'
}
export class ActionBase implements Action { readonly type : any; payload: any}
export class AddItemClass extends ActionBase {
readonly type = ToDoActions.Add ;
constructor(public payload : any) {
super();
}
}
export class RemoveItemClass extends ActionBase {
readonly type = ToDoActions.Remove ;
constructor(public payload : any) {
super();
}
}
减速器:
import { ToDoItem } from "./models";
import { ActionBase ,ToDoActions} from "./todo.actions";
export interface ToDoState
{
items : ToDoItem [] ;
}
const initialstate: ToDoState = <ToDoState>{items : [new ToDoItem('todo item', false)]};
export function todoReducer(state : ToDoState = initialstate, action: ActionBase) : ToDoState {
switch (action.type) {
case ToDoActions.Add :
return {...state, items : [...state.items, action.payload]} ;
default :
return state ;
}
}
应用组件:
import { Component } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { ToDoItem, TodoList } from './models';
import { AddItemClass } from './todo.actions';
import { ToDoState } from './todo.reducer';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
todoList: Observable<ToDoItem[]> ;
constructor(private store :Store<ToDoState>)
{
this.todoList = this.store.pipe(select('items')) ;
}
}
应用模块:
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { StoreModule } from '@ngrx/store';
import { AppComponent } from './app.component';
import { todoReducer } from './todo.reducer';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,FormsModule, StoreModule.forRoot(todoReducer)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
风景
<div>
<table class="table table-striped table-bordered table-sm" >
<tr>
<th>Name</th>
<th>status</th>
</tr>
<tr *ngFor="let item of todoList| async">
<td>{{item.taskVal}}</td>
<td>{{item.completed}}</td>
</tr>
</table>
</div>
问题是减速器配置错误,此修复
import { ToDoItem } from "./models";
import { ActionBase ,ToDoActions} from "./todo.actions";
export interface ToDoState
{
items : ToDoItem [] ;
}
const initialstate: ToDoItem= [new ToDoItem('todo item', false)];
export function todoReducer(state : TotoItem[]= initialstate, action: ActionBase) : any{
switch (action.type) {
case ToDoActions.Add :
return {...state, items : [...state.items, action.payload]} ;
default :
return state ;
}
}
模块应该已经配置好了
StoreModule.forRoot({items:todoReducer})
我有这个使用 Ngrx 的基本 Todo 应用程序,但我正在努力让它工作。我无法获得初始状态,即要显示的 TodoItems 数组。
动作:
import { Action } from "@ngrx/store";
import { ToDoItem } from "./models";
export enum ToDoActions {
Add = 'Add customer' ,
Remove = 'Remove customer'
}
export class ActionBase implements Action { readonly type : any; payload: any}
export class AddItemClass extends ActionBase {
readonly type = ToDoActions.Add ;
constructor(public payload : any) {
super();
}
}
export class RemoveItemClass extends ActionBase {
readonly type = ToDoActions.Remove ;
constructor(public payload : any) {
super();
}
}
减速器:
import { ToDoItem } from "./models";
import { ActionBase ,ToDoActions} from "./todo.actions";
export interface ToDoState
{
items : ToDoItem [] ;
}
const initialstate: ToDoState = <ToDoState>{items : [new ToDoItem('todo item', false)]};
export function todoReducer(state : ToDoState = initialstate, action: ActionBase) : ToDoState {
switch (action.type) {
case ToDoActions.Add :
return {...state, items : [...state.items, action.payload]} ;
default :
return state ;
}
}
应用组件:
import { Component } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { ToDoItem, TodoList } from './models';
import { AddItemClass } from './todo.actions';
import { ToDoState } from './todo.reducer';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
todoList: Observable<ToDoItem[]> ;
constructor(private store :Store<ToDoState>)
{
this.todoList = this.store.pipe(select('items')) ;
}
}
应用模块:
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { StoreModule } from '@ngrx/store';
import { AppComponent } from './app.component';
import { todoReducer } from './todo.reducer';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,FormsModule, StoreModule.forRoot(todoReducer)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
风景
<div>
<table class="table table-striped table-bordered table-sm" >
<tr>
<th>Name</th>
<th>status</th>
</tr>
<tr *ngFor="let item of todoList| async">
<td>{{item.taskVal}}</td>
<td>{{item.completed}}</td>
</tr>
</table>
</div>
问题是减速器配置错误,此修复
import { ToDoItem } from "./models";
import { ActionBase ,ToDoActions} from "./todo.actions";
export interface ToDoState
{
items : ToDoItem [] ;
}
const initialstate: ToDoItem= [new ToDoItem('todo item', false)];
export function todoReducer(state : TotoItem[]= initialstate, action: ActionBase) : any{
switch (action.type) {
case ToDoActions.Add :
return {...state, items : [...state.items, action.payload]} ;
default :
return state ;
}
}
模块应该已经配置好了
StoreModule.forRoot({items:todoReducer})