【发布时间】:2020-08-07 21:50:38
【问题描述】:
我有 Angular 8 应用程序。我正在使用 ngrx 进行状态管理。
但问题是,如果我尝试删除项目,它将重定向到其他选项卡。而不是删除项目。
所以我有这个:
减速器:
const intialState: Tutorial = {
name: 'initial State',
url: 'http://google.com'
};
export function tutorialReducer(state: Tutorial[] = [intialState], action: TutorialActions.Actions) {
switch (action.type) {
case TutorialActions.ADD_TUTORIAL:
return [...state, action.payload];
case TutorialActions.DELETE_TUTORIAL:
state.splice(action.payload, 1);
return state;
default:
return state;
}
}
行动:
export class AddTutorial implements Action {
readonly type = ADD_TUTORIAL;
constructor(public payload: Tutorial) {}
}
export class RemoveTutorial implements Action {
readonly type = DELETE_TUTORIAL;
constructor(public payload: number) {}
}
export type Actions = AddTutorial | RemoveTutorial;
并删除模板:
<div class="right" *ngIf="tutorials$">
<h3>Tutorials</h3>
<ul>
<li (click)="delTutorial(i)" *ngFor="let tutorial of tutorials$ | async; let i = index">
<a [href]="tutorial.url" target="_blank">{{ tutorial.name }}</a>
</li>
</ul>
</div>
和ts代码:
export class ReadComponent implements OnInit {
tutorials$: Observable<Tutorial[]>;
constructor(private store: Store<AppState>) {
this.tutorials$ = this.store.select('tutorial');
}
delTutorial(index){
this.store.dispatch(new TutorialActions.RemoveTutorial(index));
}
ngOnInit() {
}
}
和 app.module.ts:
imports: [
BrowserModule,
StoreModule.forRoot({tutorial: tutorialReducer}),
AppRoutingModule
],
但是它并没有删除项目,而是实际上打开了一个新标签。
然后我得到这个错误:
core.js:9110 ERROR TypeError: Cannot assign to read only property '5' of object '[object Array]'
at Array.splice (<anonymous>)
at tutorialReducer (tutorial.reducers.ts:16)
at combination (store.js:303)
at store.js:1213
at store.js:38
那么我必须改变什么?这样您就可以从列表中删除一个项目?
谢谢
【问题讨论】:
标签: javascript angular typescript rxjs ngrx