从组件分派操作时未调用 ngrx 效果

2023-12-29

我遇到了 ngrx 存储问题,没有发送一个动作来处理它。

这是尝试分派的组件:

  signin() {
    this.formStatus.submitted = true;
    if (this.formStatus.form.valid) {
      this.store.dispatch(new StandardSigninAction(this.formStatus.form.value.credentials));
    }
  }

行动:

export const ActionTypes = {
  STANDARD_SIGNIN: type('[Session] Standard Signin'),
  LOAD_PERSONAL_INFO: type('[Session] Load Personal Info'),
  LOAD_USER_ACCOUNT: type('[Session] Load User Account'),
  RELOAD_PERSONAL_INFO: type('[Session] Reload Personal Info'),
  CLEAR_USER_ACCOUNT: type('[Session] Clear User Account')
};

export class StandardSigninAction implements Action {
  type = ActionTypes.STANDARD_SIGNIN;

  constructor(public payload: Credentials) {
  }
}
...

export type Actions
  = StandardSigninAction
  | LoadPersonalInfoAction
  | ClearUserAccountAction
  | ReloadPersonalInfoAction
  | LoadUserAccountAction;

效果:

  @Effect()
  standardSignin$: Observable<Action> = this.actions$
    .ofType(session.ActionTypes.STANDARD_SIGNIN)
    .map((action: StandardSigninAction) => action.payload)
    .switchMap((credentials: Credentials) =>
      this.sessionSigninService.signin(credentials)
        .map(sessionToken => {
          return new LoadPersonalInfoAction(sessionToken);
        })
    );

我可以在调试中看到该组件确实调用了调度方法。我也可以确认StandardSigninAction确实被实例化了,因为构造函数中的断点被命中。

But the standardSignin$效果不叫...

什么可能导致效果不被调用?

如何调试商店内发生的情况?

有人可以帮忙吗?

附:我在导入中按如下方式运行上述效果:

EffectsModule.run(SessionEffects),

edit:这是我的 SessionSigninService.signin 方法(确实返回一个 Observable)

  signin(credentials: Credentials) {
    const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'});
    const options = new RequestOptions({headers: headers});
    const body = 'username=' + credentials.username + '&password=' + credentials.password;
    return this.http.post(this.urls.AUTHENTICATION.SIGNIN, body, options).map(res => res.headers.get('x-auth-token'));
  }

这不是一个明确的答案,但希望它会有所帮助。

在你开始之前:

  • 确保您使用的是最新版本@ngrx包(适合您正在使用的 Angular 版本)。
  • 如果您更新了任何软件包,请确保重新启动开发环境(即重新启动捆绑器、服务器等)

如果您还没有这样做,您应该看看的实施Store https://github.com/ngrx/store/blob/v2.2.1/src/store.ts- 这样您就可以对可能出现的问题做出一些有根据的猜测。注意的是,Store很轻。它既是可观察的(使用状态作为其源)又是观察者(服从调度程序)。

如果你看store.dispatch https://github.com/ngrx/store/blob/v2.2.1/src/store.ts#L35-L37你会发现它是一个别名store.next https://github.com/ngrx/store/blob/v2.2.1/src/store.ts#L35-L37,这称为next on the Dispatcher https://github.com/ngrx/store/blob/v2.2.1/src/dispatcher.ts#L8-L22.

所以调用:

this.store.dispatch(new StandardSigninAction(this.formStatus.form.value.credentials));

应该只看到调度程序发出的操作。

The Actions https://github.com/ngrx/effects/blob/v2.0.2/src/actions.ts#L11-L38注入效果中的 observable 也相当轻。这只是一个使用Dispatcher作为其来源。

要查看效果中流动的操作,您可以替换以下内容:

@Effect()
standardSignin$: Observable<Action> = this.actions$
  .ofType(session.ActionTypes.STANDARD_SIGNIN)

有了这个:

@Effect()
standardSignin$: Observable<Action> = this.actions$
  .do((action) => console.log(`Received ${action.type}`))
  .filter((action) => action.type === session.ActionTypes.STANDARD_SIGNIN)

ofType不是操作员;这是一个方法,所以要添加do基于日志记录,它需要替换为filter.

日志记录到位后,如果您收到操作,则说明效果的实现存在问题(或者操作类型的字符串/常量可能不是您认为的那样,并且某些内容不匹配)。

如果效果没有收到调度的操作,最可能的解释是store您通过它发送StandardSigninAction不一样吗store您的效果正在使用 - 也就是说,您有 DI 问题。

如果是这种情况,您应该看看与其他的有何不同SessionEffects你说的正在工作。 (至少你有一些东西可以工作,这是开始试验的好地方。)它们是从不同的模块分派的吗?是调度的模块StandardSigninAction功能模块?

如果你破解其中一个正在运行的程序会发生什么SessionEffects将其调度操作替换为StandardSigninAction?那么效果运行了吗?

请注意,此答案末尾的问题不是我想要回答的问题;而是我想要回答的问题。这些是你应该问自己并进行调查的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从组件分派操作时未调用 ngrx 效果 的相关文章

随机推荐