angular-redux / store

Angular 2+ bindings for Redux
MIT License
1.34k stars 202 forks source link

Navigate to different page after action complete #230

Closed ArielGueta closed 8 years ago

ArielGueta commented 8 years ago

I an using redux-observable like in your example but I don't know how to redirect the user to a different page after the login action success.

I am trying to Inject the Router to SessionEpics but it's throwing an error:

Bootstrap at least one component before injecting Router

spock123 commented 8 years ago

So there are basically two ways you can do this.

  1. Add a status field in your store, indicating that the user has logged on. I usually make all the session data part of my store. Then your login-component subscribes to that part of the store, and reacts to changes. This will require you to inject the Router into your component.
  2. Make routing part of your Actions. So for example, in your action creator, after a successful login has been performed, update your store , and then perform routing.

I would recommend you to take a look at https://github.com/dagstuan/ng2-redux-router which makes this much easier. The current route becomes part of the store, and you can navigate to other pages by dispatching route actions.

Also, to batch several actions into one bunch, before notifying subscribers, take a look at https://github.com/tshelburne/redux-batched-actions

Hope this helps you .. cheers

ArielGueta commented 8 years ago

Thank you for your answer, I do have a session reducer to manage my session status.

I will try your solutions.

SethDavenport commented 8 years ago

Hi @arielgueta - I have also seen that error in the situation you describe.

It's due to the peculiar way in which router configuration and NgModule execution happen during Angular2's initialisation sequence.

A quick fix if you want to continue the observable approach is to move your NgRedux.configureStore call into your app's top level component instead of in the NgModule constructor itself. That will allow Angular2 to boot thing in the right order.

It's unfortunate that router works this way; I believe it's a side effect of the way NgModules and lazy-loaded routes are implemented.

ArielGueta commented 8 years ago

Thanks for the suggestion. I do prefer to continue the observable approach so I will try your solution.

Raviraj20 commented 7 years ago

hi need a help can any one help me i want to redirect to a next page on the click of the login button in angular 2 project

andreeapNRO commented 6 years ago

@spock123

2. and then perform routing

so you mean to use the router directly in the actions creator service?