Javascript NgRx效果与无限循环碰撞角度应用程序

Javascript NgRx效果与无限循环碰撞角度应用程序,javascript,angular,typescript,ngrx,ngrx-effects,Javascript,Angular,Typescript,Ngrx,Ngrx Effects,我是NGRx的新手,我正在尝试在我的小项目上使用效果,因为date使用了外部数据,我决定将其放入效果中,所以应用程序现在在运行“npm start”后被无限循环崩溃,它只是停止工作。这是到回购的链接 count.effects.ts count.ts count.actions.ts count.components.ts 从'@angular/core'导入{Component}; 导入{Store,从'@ngrx/Store'中选择}; 从“./reducers/count/count.re

我是NGRx的新手,我正在尝试在我的小项目上使用效果,因为date使用了外部数据,我决定将其放入效果中,所以应用程序现在在运行“npm start”后被无限循环崩溃,它只是停止工作。这是到回购的链接

count.effects.ts count.ts count.actions.ts count.components.ts
从'@angular/core'导入{Component};
导入{Store,从'@ngrx/Store'中选择};
从“./reducers/count/count.reducer”导入{ICountState};
从“rxjs”导入{Observable};
从“rxjs/operators”导入{map};
从“./reducers/count/count.selectors”导入{selectCount,selectUpdatedAt};
进口{
CountIncreaseAction,
采取行动,
CountClearAction,
}来自“./reducers/count/count.actions”;
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.scss'],
})
导出类AppComponent{
public count$:Observable=this.store$.pipe(select(selectCount));
public isButtonDisabled$:Observable=this.count$.pipe(

map((count)=>count这很简单,当您发送一些操作时,会触发您的效果,包括
countActionsType.updatedAt
。 在效果内部,您发送一个名为
CountUpdatedAtAction
的操作,其类型为
countActionsType.updatedAt

这就是你的无限循环:)

每次更新时,效果都会尝试一次又一次地更新:p

要实现此功能,只需在以下位置删除
countActionsType.updatedAt

ofType(
        countActionsType.increase,
        countActionsType.decrease,
        countActionsType.clear,
        countActionsType.updatedAt // <--- remove this
      ),
类型的
(
countActionsType.增加,
countActionsType.减少,
countActionsType.clear,

countActionsType.updatedAt//先生,我只想说谢谢你,你让我度过了这一天,我花了1.5天的时间才意识到问题所在,所以在创建效果后,我所有的动作都会生效?每次发送(…)
类型的
中的动作时,效果也会被触发,是的。
import { CountActions, countActionsType } from './count.actions';

export const COUNT_NODE = 'count';

export interface ICountState {
  count: number;
  updatedAt: number;
}

const initialState: ICountState = {
  count: 0,
  updatedAt: Date.now(),
};

export const countReducer = (state = initialState, actions: CountActions) => {
  switch (actions.type) {
    case countActionsType.increase:
      return {
        ...state,
        count: state.count + 1,
      };
    case countActionsType.decrease:
      return {
        ...state,
        count: state.count - 1,
      };
    case countActionsType.clear:
      return {
        ...state,
        count: 0,
      };
    case countActionsType.updatedAt:
      return {
        ...state,
        updatedAt: actions.payload.updatedAt,
      };
    default:
      return state;
  }
};
import { Action } from '@ngrx/store';

export enum countActionsType {
  increase = '[COUNT] increase',
  decrease = '[COUNT] decrease',
  clear = '[COUNT] clear',
  updatedAt = '[COUNT] updated at',
}

export class CountIncreaseAction implements Action {
  readonly type = countActionsType.increase;
}

export class CountDecreaseAction implements Action {
  readonly type = countActionsType.decrease;
}

export class CountClearAction implements Action {
  readonly type = countActionsType.clear;
}

export class CountUpdatedAtAction implements Action {
  readonly type = countActionsType.updatedAt;

  constructor(
    public payload: {
      updatedAt: number;
    }
  ) {}
}

export type CountActions =
  | CountIncreaseAction
  | CountDecreaseAction
  | CountClearAction
  | CountUpdatedAtAction;
import { Component } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { ICountState } from './reducers/count/count.reducer';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { selectCount, selectUpdatedAt } from './reducers/count/count.selectors';
import {
  CountIncreaseAction,
  CountDecreaseAction,
  CountClearAction,
} from './reducers/count/count.actions';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  public count$: Observable<number> = this.store$.pipe(select(selectCount));
  public isButtonDisabled$: Observable<boolean> = this.count$.pipe(
    map((count) => count <= 0)
  );
  public updatedAt$: Observable<number> = this.store$.pipe(
    select(selectUpdatedAt)
  );

  constructor(private store$: Store<ICountState>) {}

  increase() {
    this.store$.dispatch(new CountIncreaseAction());
  }

  decrease() {
    this.store$.dispatch(new CountDecreaseAction());
  }

  clear() {
    this.store$.dispatch(new CountClearAction());
  }
}
ofType(
        countActionsType.increase,
        countActionsType.decrease,
        countActionsType.clear,
        countActionsType.updatedAt // <--- remove this
      ),