我有一个 angular5 组件,我正在尝试与 reduxstore 连接,它看起来像这样:
import { Component } from '@angular/core';
import { NgRedux, select, DevToolsExtension } from '@angular-redux/store';
import { TodoActions } from './actions';
import { AppState, INITIAL_STATE, rootReducer } from './store';
import {Observable} from "rxjs/Observable";
@Component({
selector: 'app-root',
template: `
<input type="text" #edit />
<button (click)="actions.add({data:edit,action:'ADD'})">add</button>
<p>The list is:</p>
<ul>
<li *ngFor="let item of (items | async)">
{{item}}
</li>
</ul>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
@select() readonly items$: Observable<string[]>;
constructor(
ngRedux: NgRedux<AppState>,
devTools: DevToolsExtension,
private actions: TodoActions) {
ngRedux.configureStore(
rootReducer,
INITIAL_STATE,
null,
devTools.isEnabled() ? [ devTools.enhancer() ] : []);
}
}
问题是我无法在列表中显示项目。这是rootreducer:
import {Action} from 'redux';
import {TodoActions} from './actions';
export interface AppState {
items: string[];
}
export const INITIAL_STATE: AppState = {items: []};
export function rootReducer(state: AppState, action: Action): AppState {
switch (action.type) {
case TodoActions.ADD:
var newstate = state;
newstate.items.push(action.data.data.value);
return newstate;
}
;
default:
return state;
}
}
如何显示项目?看起来第一项是从 redux 控制台添加到状态的。这里也是一个github链接