ngrx effect not being called when action is dispatched from component

前端 未结 5 1825
情书的邮戳
情书的邮戳 2021-02-12 11:35

I am having an issue with the ngrx store not dispatching an action to the effect supposed to deal with it.

Here is the component that tries to dispatch:



        
5条回答
  •  醉梦人生
    2021-02-12 11:48

    This is not going to be a definitive answer, but, hopefully, it will be helpful.

    Before you start:

    • Make sure you are using the latest versions of the @ngrx packages (that are appropriate for the version of Angular you are using).
    • If you've updated any packages, make sure you re-start your development environment (that is, restart the bundler, the server, etc.)

    If you've not done so already, you should have a look at the implementation of the Store - so that you make some educated guesses as to what could be going wrong. Note is that the Store is pretty light. It's both an observable (using the state as its source) and an observer (that defers to the dispatcher).

    If you look at store.dispatch you'll see that it's an alias for store.next, which calls next on the Dispatcher.

    So calling:

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

    should just see an action emitted from the dispatcher.

    The Actions observable that's injected into your effects is also pretty light. It's just an observable that uses the Dispatcher as its source.

    To look at the actions that are flowing through the effect, you could replace this:

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

    with this:

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

    ofType is not an operator; it's a method, so to add do-based logging, it needs to be replaced with a filter.

    With the logging in place, if you are receiving the action, there is something wrong with the effect's implementation (or maybe the action types' strings/constants aren't what you think they are and something is mismatched).

    If the effect is not receiving the dispatched action, the most likely explanation would be that the store through which you are dispatching the StandardSigninAction is not that same store that your effect is using - that is, you have a DI problem.

    If that is the case, you should look at what differs from the other SessionEffects that you say are working. (At least you have something working, which is a good place to start experimenting.) Are they dispatched from a different module? Is the module that dispatches StandardSigninAction a feature module?

    What happens if you hack one of the working SessionEffects to replace its dispatched action with StandardSigninAction? Does the effect then run?

    Note that the questions at the end of this answer aren't questions that I want answered; they are questions that you should be asking yourself and investigating.

提交回复
热议问题