为什么计数器值只在第一次递增和递减?
why counter value increment and decrement only first time?
https://stackblitz.com/edit/angular-q8nsfz?file=src%2Fapp%2Fapp.component.ts
import {Component, OnInit} from '@angular/core';
import {Store} from '@ngrx/store';
import {Observable} from 'rxjs';
import * as fromApp from './app.store';
import {DecrementCounter, IncrementCounter} from './store/counter.action';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
c: Observable<object>;
constructor(private store: Store<fromApp.AppState>) {
}
incrementCounter() {
this.store.dispatch(new IncrementCounter());
}
decrementCounter() {
this.store.dispatch(new DecrementCounter());
}
ngOnInit(){
this.c =this.store.select('counterValue');
}
}
嗨
你能告诉我为什么我的计数器值只在第一次递增和递减吗?我有两个按钮 increment
和 decrement
单击按钮时计数器值发生变化。但我的值只在第一次发生变化time .it 显示 0
初始值是正确的,但之后它不起作用为什么?
每次调用函数时都非常简单:incrementCounter
你创建一个新的class new IncrementCounter()
。所以每次当你调用这个函数时,它都会执行同样的事情。
您需要做的是在组件范围内创建这个新的class:
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);
}
改变你的"counterReducer function"
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;
}
https://stackblitz.com/edit/angular-q8nsfz?file=src%2Fapp%2Fapp.component.ts
import {Component, OnInit} from '@angular/core';
import {Store} from '@ngrx/store';
import {Observable} from 'rxjs';
import * as fromApp from './app.store';
import {DecrementCounter, IncrementCounter} from './store/counter.action';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
c: Observable<object>;
constructor(private store: Store<fromApp.AppState>) {
}
incrementCounter() {
this.store.dispatch(new IncrementCounter());
}
decrementCounter() {
this.store.dispatch(new DecrementCounter());
}
ngOnInit(){
this.c =this.store.select('counterValue');
}
}
嗨
你能告诉我为什么我的计数器值只在第一次递增和递减吗?我有两个按钮 increment
和 decrement
单击按钮时计数器值发生变化。但我的值只在第一次发生变化time .it 显示 0
初始值是正确的,但之后它不起作用为什么?
每次调用函数时都非常简单:incrementCounter
你创建一个新的class new IncrementCounter()
。所以每次当你调用这个函数时,它都会执行同样的事情。
您需要做的是在组件范围内创建这个新的class:
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);
}
改变你的"counterReducer function"
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;
}