Javascript 为什么计数器的值只有第一次递增和递减?

Javascript 为什么计数器的值只有第一次递增和递减?,javascript,angular,ngrx,ngrx-store,Javascript,Angular,Ngrx,Ngrx Store,从'@angular/core'导入{Component,OnInit}; 从'@ngrx/Store'导入{Store}; 从“rxjs”导入{Observable}; 从“/app.store”以应用程序的形式导入*; 从“/store/counter.action”导入{DecrementCounter,IncrementCounter}; @组成部分({ 选择器:“我的应用程序”, templateUrl:“./app.component.html”, 样式URL:['./app.com

从'@angular/core'导入{Component,OnInit};
从'@ngrx/Store'导入{Store};
从“rxjs”导入{Observable};
从“/app.store”以应用程序的形式导入*;
从“/store/counter.action”导入{DecrementCounter,IncrementCounter};
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent实现OnInit{
c:可观察的;
构造函数(私有存储:存储){
}
递增计数器(){
this.store.dispatch(new IncrementCounter());
}
递减计数器(){
this.store.dispatch(新的递减计数器());
}
恩戈尼尼特(){
this.c=this.store.select('counterValue');
}
}


你能告诉我为什么我的计数器值只有第一次递增和递减吗?我有两个按钮
increment
decreation
计数器值在点击按钮时会改变。但是我的值只有第一次改变。它显示了
0
初始值,这是正确的,但之后它就不起作用了为什么?

很简单每次调用函数时:
incrementCounter
都会创建一个新类
newincrementcounter()
。所以每次调用这个函数时,它都会执行相同的操作

您需要做的是在组件的作用域上创建这个新类:

private incrementClass = new IncrementCounter();
private decrementClass = new DecrementCounter();

  constructor(private store: Store<fromApp.AppState>) {
  }

  incrementCounter() {
     this.store.dispatch(this.incrementClass);
  }

  decrementCounter() {
    this.store.dispatch(this.decrementClass);
  }
private incrementClass=new IncrementCounter();
私有递减类=新递减计数器();
构造函数(私有存储:存储){
}
递增计数器(){
this.store.dispatch(this.incrementClass);
}
递减计数器(){
this.store.dispatch(this.decrementClass);
}

每次调用函数时都非常简单:
incrementCounter
创建一个新类
newincrementcounter()
。所以每次调用这个函数时,它都会执行相同的操作

您需要做的是在组件的作用域上创建这个新类:

private incrementClass = new IncrementCounter();
private decrementClass = new DecrementCounter();

  constructor(private store: Store<fromApp.AppState>) {
  }

  incrementCounter() {
     this.store.dispatch(this.incrementClass);
  }

  decrementCounter() {
    this.store.dispatch(this.decrementClass);
  }
private incrementClass=new IncrementCounter();
私有递减类=新递减计数器();
构造函数(私有存储:存储){
}
递增计数器(){
this.store.dispatch(this.incrementClass);
}
递减计数器(){
this.store.dispatch(this.decrementClass);
}
更改“反减速器功能”

更改“反减速器功能”


你能分享你的链接吗没有你的代码它还能工作为什么?请删除您的ansercan您可以共享您的链接吗?它可以在没有代码的情况下工作为什么?请删除你的密码你能分享商店代码吗?我想你在柜台上犯了一个愚蠢的错误。reducer.ts..用@Swooxok检查我的答案我会检查并更新你能分享商店代码吗?我想你在柜台上犯了一个愚蠢的错误。reducer.ts..用@Swooxok检查我的答案我会检查并更新使用它在没有@swooz ansersee的情况下工作它在没有@swooz anser的情况下工作
export function counterReducer(state = initialState, action: CounterActions.CounterManagment) {
  switch (action.type) {
    case CounterActions.INCREMENT:
      const counter = initialState.counter++; //see, you increment the variable initialState.counter, and then return it
      return {...state, counter};
    case CounterActions.DECREMENT:
      const currentCounter = initialState.counter--;
      return {...state, counter: currentCounter}; //idem decrement
    default :
      return state;
  }
}
Replace initialState.counter + 1 to state.counter + 1;

  switch (action.type) {
    case CounterActions.INCREMENT:
      const counter = state.counter + 1;
      return {...state, counter};
    case CounterActions.DECREMENT:
      const currentCounter = state.counter - 1;
      return {...state, counter: currentCounter};
    default :
      return state;
  }