Closed yd021976 closed 4 years ago
After some debug of the plugin, I can confirm that the "InitState" action is never "received" by the actions$ Observable in the reset handler. I don't know why.
Note that in any of my state classes, I can receive "InitState" action from NGXS.
Here the plugin code that is never executed and cause the ResetPlugin.InitialState to never be initialized with the initial store state at startup.
Source path : src/lib/reset.handler.ts The code below is never executed (lines 13 to 18)
this.actions$
.pipe(
ofActionSuccessful(InitState),
take(1),
)
.subscribe(() => (this.resetService.initialState = this.store.snapshot()));
Hi @yd021976,
I think you are not calling the static forRoot
method of NgxsResetPluginModule
which hooks into the action stream via APP_INITIALIZER
. Please call the forRoot
method as described here.
Let us know if this works.
Hi, thanks for your reply. Unfortunably i confirm i use forRoot static method of the plugin module and it still doesn’t work.
Is it called in the root module of your application? Could you please share a minimum reproduction of the issue? You can use a playground such as StackBlitz.
By the way, here is a quick playground that shows it is working.
My bad, it was very tricky to reproduce the error. I start from a scratch Angular 9 project, and finally find what was going wrong.
Simply, we MUST import the BrowserModule before importing NgxsModule.forRoot()
I just don't know why I have to do this, but now I've turn into a new problem because my app_initializer (using NGXS store) doesn't work with BrowserModule imported before NgxsModule.forRoot().
So as I've found MY error, I think you can close this issue.
Thank you for your help and quick answer
You're welcome. Have a nice day. 🙂
Hey! One year later I landed in this issue too.
I can confirm that if you move the BrowserModule
import right after the NgxsModule
the NgxsResetPluginModule
will erase all state keys except the ones to keep.
I will be back soon with an example.
You can move BrowserModule above the NgxsModule
import to let the NgxsResetPluginModule
works properly
Describe the bug When I want to reset my state to default initial state except one I use "StateResetAll", exemple
this.store.dispatch(new StateResetAll(SiteZonesState))
My store has several states, like this
When I want to reset my state to default state (init) except one, all of the keys of my state disappear except the one in my exemple. So after StateResetAll action dispatch, my state looks like :
[SiteZonesState]
instead of[ApplicationState, UserState,UsersState,TemplatesState,AclUIState,AclEntitiesState,ApplicationLocksState,ServicesState,AppNotificationsState,SiteZonesState,SiteZonesUIState]
Possible bug In debug, I see that ResetHandler constructor seem to never "catch" InitState action, and then never set initialState property of ResetService. As the resetService.initialState in your plugin is always "undefined", when I dispatch a ResetStateAll, the plugin set my state with the "undefined" state value stored in ResetService.
NGXS version : 3.6.2