【问题标题】:NgRx Effects crashing Angular app with infinite loopNgRx Effects 使用无限循环使 Angular 应用程序崩溃
【发布时间】:2020-07-25 13:16:33
【问题描述】:

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

count.effects.ts

import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import {
  countActionsType,
  CountUpdatedAtAction,
} from './reducers/count/count.actions';
import { map } from 'rxjs/operators';

@Injectable()
export class AppEffects {
  constructor(private actions$: Actions) {}

  @Effect()
  updatedAt$() {
    return this.actions$.pipe(
      ofType(
        countActionsType.increase,
        countActionsType.decrease,
        countActionsType.clear,
        countActionsType.updatedAt
      ),
      map(() => {
        return new CountUpdatedAtAction({
          updatedAt: Date.now(),
        });
      })
    );
  }
}

count.reducer.ts

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;
  }
};

count.actions.ts

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;

count.components.ts

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());
  }
}

【问题讨论】:

    标签: javascript angular typescript ngrx ngrx-effects


    【解决方案1】:

    这很简单,当你派发一些动作时会触发你的效果,包括countActionsType.updatedAt。 在您的效果中,您调度一个名为 CountUpdatedAtAction 的 Action,其类型为 countActionsType.updatedAt

    那是你的无限循环:)

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

    要完成这项工作,只需在以下位置删除 countActionsType.updatedAt

    ofType(
            countActionsType.increase,
            countActionsType.decrease,
            countActionsType.clear,
            countActionsType.updatedAt // <--- remove this
          ),
    

    【讨论】:

    • 先生,我只想对您说声谢谢,您成就了我的一天,我花了 1.5 天的时间才意识到出了什么问题,所以在创建效果后,我的所有行为都会产生效果?
    • 每次你的ofType(...)里面的action被调度,效果也会被触发,是的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    相关资源
    最近更新 更多