Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ngxs - How to prepopulate state on application load

Tags:

ngxs

I would like to set the ngxs state on application load. What would be the best way to do this?

e.g. resolve the state object in an angular APP_INITIALIZER and then ???

like image 378
maku_at Avatar asked Sep 17 '25 15:09

maku_at


1 Answers

You could make the HTTP call to load data for the state via the ngxsOnInit lifecycle hook see the lifecycle documentation here.

In my usage of NGXS we've typically had our states respond to a particular action e.g. UserLoggedIn to load their initial data from an external service

We needed to wait on this action for two reasons:

  • the HTTP services we called needed the auth token returned by Login to authenticate.
  • in many cases the services would return 'default' data that was user specific.
like image 121
Garth Mason Avatar answered Sep 21 '25 17:09

Garth Mason