繁体   English   中英

为什么 ngrx 忽略我的初始商店价值

[英]Why is ngrx ignoring my initial store value

我有这个使用 Ngrx 的基本 Todo 应用程序,但我正在努力让它工作。 我无法获得初始的 state,这是要显示的 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})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM