Reputation: 1689
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 ???
Upvotes: 4
Views: 2765
Reputation: 8011
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:
Upvotes: 2
Reputation: 31
You can either use defaults
from the stat decorator (if it's the same one all the time on startup)or just populate the state in APP_INITIALIZER
using normal store.dispatch
Upvotes: 0