ngxsOnChanges
ngxsOnInit
NgxsOnInit
interface, its ngxsOnInit
method will be invoked after all the states from the state's module definition have been initialized and pushed into the state stream. The states' ngxsOnInit
methods are invoked in a topological sorted order going from parent to child. The first parameter is the StateContext
where you can get the current state and dispatch actions as usual.ngxsAfterBootstrap
NgxsAfterBootstrap
interface, its ngxsAfterBootstrap
method will be invoked after the root view and all its children have been rendered, because Angular invokes functions, retrieved from the injector by APP_BOOTSTRAP_LISTENER
token, only after creating and attaching ComponentRef
of the root component to the tree of views.ngxsOnInit()
and whenever state changes.ngxsOnChanges()
and before the APP_INITIALIZER
token is resolved.APP_INITIALIZER
is just a token that references Promise factories. If you've ever used the APP_INITIALIZER
token, then you are already familiar with its syntax:ApplicationInitStatus
class. What does Angular do under the hood? It gets an instance of this class and invokes the runInitializers
method:APP_INITIALIZER
array (as it's a multi
token) and invoking those factories, that you've provided in useFactory
properties:asyncInitPromises
are provided into Promise.all
. That's all the magic. That's why the bootstrapModule
returns a Promise
:APP_INITIALIZER
is resolved after NGXS states are initialized. They are initialized by the NgxsModule
that is imported into the AppModule
. The ngxsOnInit
method on states is also invoked before the APP_INITIALIZER
token is resolved. Given the following code:getVersion
method is invoked before the version
property is set. Why? Because the ngxsOnInit
methods on states are invoked before the APP_INITIALIZER
is invoked!ngxsAfterBootstrap
method:SetVersion
action right after the version is fetched:ngxsOnInit
methods that is fetched during the APP_INITIALIZER
stage.