本文介绍了在我的组件中实施Ngrx Store服务的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用户尝试解决有关该帖子的问题后:我现在正尝试使用 Ngrx存储github 可以帮助我解决多个输入/输出事件.
After a user tried to help with an issue regarding that post: My stackoverflow old post I am now trying to implement the Ngrx store using Ngrx store github to help me solve multiples input/output event.
就在我的构造函数之后,我出现了一个错误:
Just after my constructor I have an error:
child.ts :
import { Component, Input, Output, EventEmitter } from '@angular/core';
import { UserService3 } from '../user3.service';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { ON, OFF, RESET } from '../counter';
interface AppState {
counter: boolean;
}
@Component({
selector: 'my-daydetail',
templateUrl: './my-daydetail.component.html',
styleUrls: ['./my-daydetail.component.css']
})
export class MyDaydetailComponent {
counter: Observable<boolean>;
constructor(private store: Store<AppState>) {
this.counter = store.select('counter');
}
//...
}
counter.ts
import { Action } from '@ngrx/store';
export const ON = 'ON';
export const OFF = 'OFF';
export const RESET = 'RESET';
export function counterReducer(state: boolean = true, action: Action) {
switch (action.type) {
case ON:
return false;
case OFF:
return true;
case RESET:
return 0;
default:
return state;
}
}
推荐答案
您已在化简器中将初始状态设置为false.将其设为任意,并使用空对象对其进行初始化.同样在用于ON/OFF/RESET等的开关情况下,您还必须返回不可变状态,如下所示:
You have set the initial state to false in your reducer. Make it any and initialize it with empty object. Also in your switch case for ON/OFF/RESET etc. you have to return immutable state like below:
return {...state, counter:true/false/0}
这篇关于在我的组件中实施Ngrx Store服务的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!